setInterval 在 if 条件中包含警报时起作用,否则仅起作用一次
这个问题可能看起来是之前提出的类似问题,但事实并非如此。
我的问题如下: 我的代码片段如下:
var i = 0;
function func2() {
if (i==0){
document.getElementById('frame1').contentWindow.opts[selected].setAttribute('name','channels');
var strValue = document.getElementById('frame1').contentWindow.opts[selected].getAttribute('name');
//alert("Attribute value set :" + strValue);
document.getElementById('frame1').contentWindow.handleKeyCode(VK_ENTER);
i++;
}
else if(i==1){
// For Channels section -- "Get current channel details"
document.getElementById('frame1').contentWindow.opts[selected].setAttribute('name','get');
var strValue = document.getElementById('frame1').contentWindow.opts[selected].getAttribute('name');
//alert("Attribute value set :" + strValue);
document.getElementById('frame1').contentWindow.handleKeyCode(VK_ENTER);
i++;
}
else if(i==2){
// For Channels section -- "Set current channel details"
document.getElementById('frame1').contentWindow.opts[selected].setAttribute('name','set');
document.getElementById('frame1').contentWindow.menuSelect(1);
var strValue = document.getElementById('frame1').contentWindow.opts[selected].getAttribute('name');
//alert("Attribute value set :" + strValue);
document.getElementById('frame1').contentWindow.handleKeyCode(VK_ENTER);
i++;
}
}
$(function() {
setInterval(function(){func2();}, 1000);
});
在此 setInterval 只工作一次,即它只执行第一个 if 条件。但当我启用所有条件的“警报”消息时,setInterval 工作正常。
我不想要此 setInterval if 条件的“警报消息”,它应该根据条件并在给定的时间间隔执行每个 if elseif 语句。
我有能力实现这一目标。另外,我在这个论坛中没有找到与 setInterval 相关的问题的任何令人满意的参考。请帮忙。
谢谢。
this question might be seems to be the similar question asked previously but its not.
My question is following :
I have code snippet as follows :
var i = 0;
function func2() {
if (i==0){
document.getElementById('frame1').contentWindow.opts[selected].setAttribute('name','channels');
var strValue = document.getElementById('frame1').contentWindow.opts[selected].getAttribute('name');
//alert("Attribute value set :" + strValue);
document.getElementById('frame1').contentWindow.handleKeyCode(VK_ENTER);
i++;
}
else if(i==1){
// For Channels section -- "Get current channel details"
document.getElementById('frame1').contentWindow.opts[selected].setAttribute('name','get');
var strValue = document.getElementById('frame1').contentWindow.opts[selected].getAttribute('name');
//alert("Attribute value set :" + strValue);
document.getElementById('frame1').contentWindow.handleKeyCode(VK_ENTER);
i++;
}
else if(i==2){
// For Channels section -- "Set current channel details"
document.getElementById('frame1').contentWindow.opts[selected].setAttribute('name','set');
document.getElementById('frame1').contentWindow.menuSelect(1);
var strValue = document.getElementById('frame1').contentWindow.opts[selected].getAttribute('name');
//alert("Attribute value set :" + strValue);
document.getElementById('frame1').contentWindow.handleKeyCode(VK_ENTER);
i++;
}
}
$(function() {
setInterval(function(){func2();}, 1000);
});
In this the setInterval works only once i.e it executes only the first if condition. But as I enables the "alert" messages of all the conditions then the setInterval works fine.
I don't want the "Alert Messages" for this setInterval if conditions, it should executed each if elseif statment as per the condition and at given time interval.
I am enable to achieve this. Also i didn't find any satisfactory reference from the setInterval related questions in this forum. Please help.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信您在
func2
内遇到了异常,这使得它看起来好像没有运行多次。当 .contenWindow 为 null 时读取它。尝试使用 try/catch 捕获异常。或者使用调试器。I believe you are getting an exception inside
func2
which make it seem like it is not running more than once. Reading .contenWindow when it is null. Try using try/catch to catch the exception. Or use a debugger.