【问题标题】:javascript setTimeout timer step functionjavascript setTimeout 定时器步进函数
【发布时间】:2015-08-01 06:26:48
【问题描述】:

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

【问题讨论】:

  • 怎么不工作了?出了什么问题?控制台有错误吗?
  • 我终于弄明白了,只需要操纵我的一些动画逻辑。我能够通过首先调用计时器暂停函数,将我的动画布尔变量设置为 false,然后手动调用我的绘图动画函数一次而不是被超时调用来实现我的结果。

标签: javascript jquery animation timer settimeout


【解决方案1】:

您可以使用 Interval 代替 Timeout,它会像一个步骤一样重复,直到调用 clearInterval()。

【讨论】:

  • 感谢您的帮助,我不知道间隔!
猜你喜欢
  • 1970-01-01
  • 2013-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多