javascript setTimeout定时器步函数
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();
}
您可以使用间隔而不是超时,它将像步骤一样重复,直到调用 clearInterval()。
我正在尝试使用自定义计时器创建一个函数,它将使我的动画前进一帧 运行。我的计时器现在可以暂停和恢复,但我还没有想出一种方法来使步进功能正常工作。我的动画超时延迟设置为 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();
}
您可以使用间隔而不是超时,它将像步骤一样重复,直到调用 clearInterval()。