javascript定时循环w/setTimeout不工作
我正在尝试编写一个脚本,每秒更新一个iframe。这就是我到目前为止所看到的,出于某种原因,它会加载一段时间,然后显示数字14541+/-50左右。“某物”和“其他某物”也从未出现在屏幕上 它停在14541是因为一些内置的浏览器防止无限循环还是什么?为什么计时器不能正常工作javascript定时循环w/setTimeout不工作,javascript,loops,timer,Javascript,Loops,Timer,我正在尝试编写一个脚本,每秒更新一个iframe。这就是我到目前为止所看到的,出于某种原因,它会加载一段时间,然后显示数字14541+/-50左右。“某物”和“其他某物”也从未出现在屏幕上 它停在14541是因为一些内置的浏览器防止无限循环还是什么?为什么计时器不能正常工作 var c = 0; var t; timer(); document.write("something"); function timer(){ if(t) { window.clearTimeout(t) }
var c = 0;
var t;
timer();
document.write("something");
function timer(){
if(t) { window.clearTimeout(t) }
update_preview();
c++;
t=setTimeout(timer(), 1000);
document.write("something else");
}
function update_preview(){
prev = window.open("","preview");
prev.document.open();
prev.document.writeln("<html><head><title>live preview window</title></head><body>");
prev.document.writeln(c);
prev.document.writeln("</body></html>");
prev.document.close();
}
var c=0;
变量t;
定时器();
记录。写(“某物”);
函数计时器(){
if(t){window.clearTimeout(t)}
更新_预览();
C++;
t=设置超时(计时器(),1000);
记录。写(“其他东西”);
}
函数更新_预览(){
prev=窗口打开(“,”预览“);
prev.document.open();
prev.document.writeln(“实时预览窗口”);
上一份书面文件(c);
上一份书面文件(“”);
上一个document.close();
}
在定时器之后移除()
通过放置timer()
,可以立即调用timer
函数,并将返回值指定为(未定义的
,因为该函数中没有返回任何内容)作为计时器运行时执行的函数
相反,您应该只放置计时器
。这会将函数本身(而不是其返回值)传递给计时器用完时调用的函数
另外,为了清楚起见,我建议将函数timer(){…}
位放在对timer()
的初始调用之前。JavaScript会自动为您执行此操作(称为“提升”),但如果您自己先将函数定义放在首位,则更容易理解。在这一行t=setTimeout(timer(),1000)
您没有将定时器
传递到setTimout,而是传递定时器
的结果。您应该使用t=setTimeout(计时器,1000)代码>。您不能将方法计时器作为函数计时器的参数调用。您必须作为参数函数计时器传递:
设置超时(定时器,1000)
不要使用document.write已过时。查看DOM和jQuery,以便动态地处理页面。使用它
t=setTimeout(function(){timer();}, 1000);
或
或
最后一个选项不应使用。没有显示“其他内容”的原因是,当您调用setTimeout并传入函数时,计时器
函数会立即处理。同样的事情也发生在函数内部。因此,您正在创建一个非终止无限递归循环,这将使您的浏览器无响应
由于每次运行timer
功能(每1秒一次)时都在timer
上运行setTimeout
,因此应使用setInterval
,而不是使用setTimeout
因此,您的代码如下所示:
var c = 0;
var t;
timer();
// call setInterval here to run the timer function every 1 second
t=setInterval('timer()', 1000);
document.write("something");
function timer(){
if(t) { window.clearTimeout(t) }
update_preview();
c++;
document.write("something else");
}
function update_preview(){
prev = window.open("","preview");
prev.document.open();
prev.document.writeln("<html><head><title>live preview window</title></head><body>");
prev.document.writeln(c);
prev.document.writeln("</body></html>");
prev.document.close();
}
var c=0;
变量t;
定时器();
//在此处调用setInterval以每1秒运行一次计时器函数
t=设置间隔('timer()',1000);
记录。写(“某物”);
函数计时器(){
if(t){window.clearTimeout(t)}
更新_预览();
C++;
记录。写(“其他东西”);
}
函数更新_预览(){
prev=窗口打开(“,”预览“);
prev.document.open();
prev.document.writeln(“实时预览窗口”);
上一份书面文件(c);
上一份书面文件(“”);
上一个document.close();
}
因此,现在不是每1秒运行一次setTimeout,而是运行一次setInterval,它将每1秒处理一次。最好使用setInterval而不是setTimeout。您能详细说明一下吗?我修正了计时器,现在不知何故它在0工作,然后重定向到一个新页面,在那里它一遍又一遍地打印“其他东西”,弹出窗口显示不断增加的数字。
t=setTimeout("timer()", 1000);
var c = 0;
var t;
timer();
// call setInterval here to run the timer function every 1 second
t=setInterval('timer()', 1000);
document.write("something");
function timer(){
if(t) { window.clearTimeout(t) }
update_preview();
c++;
document.write("something else");
}
function update_preview(){
prev = window.open("","preview");
prev.document.open();
prev.document.writeln("<html><head><title>live preview window</title></head><body>");
prev.document.writeln(c);
prev.document.writeln("</body></html>");
prev.document.close();
}