javascript设置超时计时器步长函数

javascript设置超时计时器步长函数,javascript,jquery,animation,timer,settimeout,Javascript,Jquery,Animation,Timer,Settimeout,我正在尝试创建一个使用自定义计时器的函数,它将我正在运行的动画提前一帧。我的计时器现在可以暂停和恢复,但我还没有找到一种方法让步进函数工作。我的动画超时延迟设置为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 ap

我正在尝试创建一个使用自定义计时器的函数,它将我正在运行的动画提前一帧。我的计时器现在可以暂停和恢复,但我还没有找到一种方法让步进函数工作。我的动画超时延迟设置为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()。

它是如何工作的?出了什么问题?控制台中有错误吗?我最终只通过操纵一些动画逻辑就解决了这个问题。我首先调用计时器暂停函数,将动画布尔变量设置为false,然后手动调用绘图动画函数一次,而不是通过超时调用。感谢您的帮助,我不知道间隔!