Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript定时循环w/setTimeout不工作_Javascript_Loops_Timer - Fatal编程技术网

javascript定时循环w/setTimeout不工作

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) }

我正在尝试编写一个脚本,每秒更新一个iframe。这就是我到目前为止所看到的,出于某种原因,它会加载一段时间,然后显示数字14541+/-50左右。“某物”和“其他某物”也从未出现在屏幕上

它停在14541是因为一些内置的浏览器防止无限循环还是什么?为什么计时器不能正常工作

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();
}