[英]How do I pause and resume multiple setTimeout in JavaScript?
I am stuck into something which I can't seem to figure out.我陷入了我似乎无法弄清楚的事情。 I am trying to pause and resume an animation using multiple
setTimeout
as it's clear from the code but it doesn't work for me.I took the code from: javascript: pause setTimeout();我正在尝试使用多个
setTimeout
暂停和恢复 animation,因为从代码中可以清楚地看出,但它对我不起作用。我从以下位置获取代码: javascript: pause setTimeout(); I edited the code according to my program but it doesn't seem to work.It just starts the animation from the very first state for eg I paused at var gT=setTimeout
but it starts from var yT=setTimeout
My code looks like this:我根据我的程序编辑了代码,但它似乎不起作用。它只是从第一个 state 开始 animation 例如我在
var gT=setTimeout
处暂停,但它从var yT=setTimeout
开始我的代码如下所示:
var start, remaining;
function pause() {
window.clearTimeout(yT);
window.clearTimeout(gT);
remaining -= Date.now() - start;
};
function resume() {
start = Date.now();
window.clearTimeout(yT);
window.clearTimeout(gT);
yT = window.setTimeout(function(){
all();
loop();
}, remaining);
gT = window.setTimeout(function(){
all();
loop();
}, remaining);
};
pause();
resume();
You need to declare and set remaining and start separately.您需要单独声明并设置剩余和启动。
var start_yT,start_gT, remaining_yT, remaining_gT;
function pause() {
window.clearTimeout(yT);
window.clearTimeout(gT);
remaining_yT -= Date.now() - start_yT;
remaining_gT -= Date.now() - start_gT;
};
function resume() {
start = Date.now();
window.clearTimeout(yT);
window.clearTimeout(gT);
yT = window.setTimeout(function(){
all();
loop();
}, remaining_yT);
gT = window.setTimeout(function(){
all();
loop();
}, remaining_gT);
};
pause();
resume();
And if you need to do with more advance version of pause/resume, this is the link .如果您需要更高级的暂停/恢复版本,这就是链接。 This create each timer separately runtime as we did manually.
这就像我们手动创建的那样分别创建每个计时器运行时。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.