有什么方法可以从另一个函数暂停下面的延迟函数集?我想通过执行另一个函数暂停所有内容10秒钟。
我需要的是:
function pause(){
pause sleepLoop() for 10 seconds
}
如果无法暂停以下执行,我可以将其杀死吗?
function game() {
sleepLoop();
}
function sleepLoop() {
loop...
setTimeout('gameActions()',5000);
}
function gameActions() {
actions...
sleepLoop();
}
答案 0 :(得分:1)
将计时器存储在变量中。然后你可以用clearTimeout来阻止它,并在10秒后重新启动它:
function game() {
sleepLoop();
}
var sleepLoopTimeout;
function sleepLoop() {
gameActions();
sleepLoopTimeout = setTimeout(sleepLoop,5000);
}
function pause(){
clearTimeout(sleepLoopTimeout);
setTimeout(sleepLoop, 10000);
}
function gameActions() {
// Actions
}
答案 1 :(得分:0)
var gameTimeout, sleepTimeout;
function game() {
gameTimeout = setTimeout(sleepLoop, 10000); //start in 10 seconds
}
function sleepLoop() {
sleepTimeout = setTimeout(gameActions, 5000); //start in 5 secs
}
function gameActions() {
sleepLoop();
}
//start it off:
game();
除了上述内容,我不确定你在问什么。
杀死(清除)超时:
clearTimeout(gameTimeout);
clearTimeout(sleepTimeout);
答案 2 :(得分:0)
var blocked = false;
function loopToBePaused()
{
if (blocked !== false)
{
// will check for blocker every 0.1s
setTimeout(loopToBePaused, 100);
return;
}
// payload
// normal loop now, e.g. every 1s:
setTimeout(loopToBePaused, 1000);
}
// somewhere in code run:
blocked = setTimeout(function() {
// blocking our loop'ed function for 10s
blocked = false;
}, 10000);
// this will block our main loop for 10s