定时器使用frameRate和帧计数器可靠吗? [英] Timer using frameRate and frame counter reliable?

查看:162
本文介绍了定时器使用frameRate和帧计数器可靠吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用p5js使用计时器倒计时编程动画。我将我的计时器设置为在草图中由draw()函数设置动画的对象内的每个帧中进行更新。因此,setInterval()不适用于我正在尝试的操作。

I'm using p5js to program an animation with a timer countdown. I set my timer up to be updated each frame within an object that is being animated by the draw() function in sketch. Because of this, setInterval() will not work for what I'm trying to do.

我以为我可以使用frameRate和一个帧计数器来决定是否已经过了一秒钟:

I thought I could use the frameRate and a frame counter to decide if a second has passed:

this.updateTimer = function(){
  this.framecounter++;
  if(this.framecounter > frameRate()){
    this.framecounter = 0;
    //increment seconds
  }
}

这是否可靠?我在一个实际的计时器上进行了测试,大约15秒后它似乎提前了大约1秒。有没有更好的方法通过每帧调用一个函数来做到这一点?谢谢!

Is this reliable? I tested it against an actual timer and it seems to be about 1 second ahead after about 15 seconds. Is there a better way to do this by calling a function each frame? Thanks!

推荐答案

为什么不使用 frameCount 变量?有关详细信息,请参阅参考

Why don't you just use the frameCount variable? More info is available in the reference.

您也可以使用 millis()函数。同样,参考是您最好的朋友。

You could also use the millis() function instead. Again, the reference is your best friend.

如果仍然无法正常工作,请发布 MCVE (或更好的,CodePen或JSFiddle)我们可以运行来查看问题。

If you still can't get it working, please post a MCVE (or better yet, a CodePen or JSFiddle) that we can run to see the problem.

这篇关于定时器使用frameRate和帧计数器可靠吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆