我怎么能保持我的程序继续超过setTimeout
how can I keep my program from continuing past a setTimeout?
所以我有一个递归调用自己的计时器:
function introAnimation()
{
var ostream = document.getElementById('table');
var newhtml = '<h1>Ready?</h1><br/>';
newhtml += '<h1>' + countdown-- + '</h1>';
ostream.innerHTML = newhtml;
if (countdown > 0)
var a = setTimeout(function() {introAnimation()}, 1000);
}
,但问题是程序在完成计时器之前继续运行。是否有任何方法可以保持所有其他进程,直到指定的功能停止?
是否有办法保持所有其他进程,直到指定的功能停止?
是和不是。方法是无限循环(while(true) ;
),但这是不希望的,因为它会冻结浏览器并且永远不会停止(因为超时不能拦截正在运行的函数)。所以你不应该。你真正想要请求的是:
如何在超时后延迟程序继续?
使用回调。你们已经在introAnimation
中用过了,所以应该不难。将该函数更改为
function introAnimation(callback) {
var ostream = document.getElementById('table');
var newhtml = '<h1>Ready?</h1><br/>';
newhtml += '<h1>' + countdown-- + '</h1>';
ostream.innerHTML = newhtml;
if (countdown > 0)
var a = setTimeout(function() {introAnimation(callback)}, 1000);
else
callback();
}
和你的程序从
introAnimation();
// rest of program
introAnimation(function() {
// part of the program that should run when the itro is done
});
// part of the programm that runs during the intro animation
// (might be nothing)
相关文章:
- setInteval vs setTimeout
- 如何在chrome扩展中存储数据/结果,以及如何使用setTimeout使其只被调用一次
- setTimeout可以与闭包内的函数一起使用吗
- Javascript setTimeout for an array
- 刷新后,setTimeout将工作或不工作
- 为什么继续;语句冻结浏览器
- Canvas+svg路径动画,在路径中的特定点暂停一段时间,然后继续
- 继续使用javascript中的setTimeout
- setTimeout函数能否在其前面的代码执行之前激发
- jQuery setTimeOut: in for-loop
- SetTimeout and clearTimeout in Javascript
- 在setTimeout中调用相同的函数
- 当一些承诺失败时,如何继续使用$q.all()
- setTimeout不能太长
- 我怎么能保持我的程序继续超过setTimeout
- 继续使用SetInterval,SetTimeOut和Requestanimationframe
- 即使在浏览器窗口关闭后,仍然使用setTimeout继续执行AJAX
- 是想控制我的动画的FPS一个很好的理由继续使用setTimeout而不是requestAnimationFrame
- 任何使javascript set interval或setTimeout在node.js服务器重启后继续计数的解决方案
- 如何在退出应用程序或使用cordova中的javascript重新启动设备时继续计时器(setTimeout())