[英]JavaScript - Restart setTimeout timer in a recursive function
[英]javascript setTimeout timer step function
我正在尝试使用自定义计时器创建一个函数,该函数将推进我正在运行的动画的一帧。 我现在可以让计时器暂停和恢复,但是我无法找到一种方法来使步进功能正常工作。 我的动画超时延迟设置为 25 毫秒。
这是我正在使用的内容:
延迟 = 25
//This function is a custom timer that tracks the start time of a timeout and calculates the remaining time
// in order to resume at the appropriate frame of animation.
function Timer(callback, delay) {
var timerId, start, remaining = delay;
this.pause = function() {
window.clearTimeout(timerId);
remaining -= new Date() - start;
animate = false;
};
this.resume = function() {
start = new Date();
window.clearTimeout(timerId);
timerId = window.setTimeout(callback, remaining);
animate = true;
};
this.resume();
}
我感谢任何人可以给我的任何帮助,我仍在学习并且从来没有擅长计时器或计时器事件。
更新我设法解决了我自己的问题! 这就是我最终得出的结论。
//This function is a custom timer that tracks the start time of a timeout and calculates the remaining time
// in order to resume at the appropriate frame of animation.
function Timer(callback, delay){
var timerId, start, remaining = delay;
this.pause = function(){
window.clearTimeout(timerId);
remaining -= new Date() - start;
animate = false;
};
this.resume = function(){
start = new Date();
window.clearTimeout(timerId);
timerId = window.setTimeout(callback, remaining);
animate = true;
};
this.step = function(){
timer.pause();
animate = false;
advanceAnimation(true);
};
this.resume();
}
您可以使用 Interval 代替 Timeout,它会像一个步骤一样重复,直到调用 clearInterval() 为止。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.