我正在调用这个函数
function drawLayers() {
//setTimeout(drawBlueSky,500);
//setTimeout(drawCircle1,1250);
setTimeout(drawMoon,800);
setTimeout(drawCircle1,2300);
setTimeout(drawCircle2,2700);
setTimeout(drawCircle3,3100);
setTimeout(drawCircle4,3500);
setTimeout(drawCircle5,3900);
setTimeout(drawtext2,4300);
setTimeout(drawtext,4700);
setTimeout(drawtext3,5100);
setTimeout(drawtext4,5500);
setTimeout(drawtext5,5900);
setTimeout(drawtext6,6300);
setTimeout(drawtext7,6700);
setTimeout(drawtext8,7100);
setTimeout(drawtext9,7500);
setTimeout(drawtext10,7900);
}
它调用了许多其他函数 fox ex drawMoon
,drawCircle1
等
我在点击播放按钮时调用了 drawLayers()
函数。我需要的是如果有人点击停止按钮 setTimeout
函数应该停止调用所有其他函数或停止在任何地方。例如,如果 drawMoon
函数被调用并且有人点击停止所有其他函数 drawCircle1
,则不应调用 drawCircle2
。
- 暂停按钮还有第三个按钮,单击该按钮将暂停上述 setTimeout 函数。当我再次点击同一个按钮时,它应该从它停止的地方调用函数。
这可能吗?
最佳答案
有这样的代码:
var timers = [];
function drawLayers() {
//setTimeout(drawBlueSky,500);
//setTimeout(drawCircle1,1250);
timers = [];
timers.push(setTimeout(drawMoon,800));
timers.push(setTimeout(drawCircle1,2300));
timers.push(setTimeout(drawCircle2,2700));
timers.push(setTimeout(drawCircle3,3100));
timers.push(setTimeout(drawCircle4,3500));
timers.push(setTimeout(drawCircle5,3900));
timers.push(setTimeout(drawtext2,4300));
timers.push(setTimeout(drawtext,4700));
timers.push(setTimeout(drawtext3,5100));
timers.push(setTimeout(drawtext4,5500));
timers.push(setTimeout(drawtext5,5900));
timers.push(setTimeout(drawtext6,6300));
timers.push(setTimeout(drawtext7,6700));
timers.push(setTimeout(drawtext8,7100));
timers.push(setTimeout(drawtext9,7500));
timers.push(setTimeout(drawtext10,7900));
}
function StopAll() {
for (var i = 0; i < timers.length; i++)
window.clearTimeout(timers[i]);
}
关于Javascript setTimeout 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7191769/