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

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

问题描述

我正在使用 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 和 frame counter 的定时器可靠吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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