重置requestAnimationFrame()中“时间戳”的值?

时间:2018-08-27 13:52:02

标签: javascript timestamp requestanimationframe

我目前正在制作一张幻灯片,该幻灯片可以在10秒钟后翻转幻灯片,并使用requestAnimationFrame进行幻灯片播放。

但是,时间戳有一些问题。我想跟踪时间戳值(这没问题),并且当它的值超过10000(10秒)时,将时间戳重置为0并继续运行。但是,当我尝试更改时间戳的值时,什么也没有发生。假设它是一个const?在每次调用中将一个变量设置为performance.now()也不像我期望的那样。

想知道此问题的最佳解决方法是什么,也许可以使用performance.now()?谢谢!

1 个答案:

答案 0 :(得分:1)

循环开始时,请存储对循环开始时间的引用。然后更新该变量,而不要尝试修改时间戳。

let start = null;
let loop = (timestamp) => {
  if (!start) {
    start = timestamp;
  };
  const progress = timestamp - start;
  if (progress > 10000) {
    console.log('loop', start);
    start = timestamp;
  }
  requestAnimationFrame(loop)
};
requestAnimationFrame(loop);
相关问题