如何在无限循环中执行3秒后执行代码

发布于 2025-01-22 14:42:55 字数 318 浏览 2 评论 0原文

我正在使用HTML帆布进行游戏,我想在其中每次都在动画循环中检查某些条件,如果该条件变为真实,那么我不想检查接下来的3秒钟,并且3秒钟后,我想再次检查。像

function animate(){
  requestAnimationFrame(animate)
  if (checking something){
    if this if statement gets executed then don't check this if statement for next 3 seconds 
  }

这听起来有些混乱,但无论如何都可以做到这一点。

I am making a game using html canvas in which i want to check for certain condition every time in animate loop and if that condition become true once then I don't want to check for next 3 seconds and after 3 seconds i want to check again. Like

function animate(){
  requestAnimationFrame(animate)
  if (checking something){
    if this if statement gets executed then don't check this if statement for next 3 seconds 
  }

This might sound bit confusing but is there anyway to do this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

以酷 2025-01-29 14:42:55

我的建议是添加一个标志 - 您可以用来检查是否应该完成操作的另一个变量,以及在一定时间之后切换标志的另一个功能。

// THE CHECKING FLAG
let shouldCheckCondition = true;

function animate(){
  requestAnimationFrame(animate)
  
  // CHECK THE FLAG FIRST, IF SHOULD NOT CHECK, EXIT THE FUNCTION
  if (!shouldCheckCondition) return;

  if (condition){
    // DISABLE THE CHECKING FLAG SO YOU SKIP THE SUBSEQUENT CHECKS
    shouldCheckCondition = false;

    // DO SOMETHING WHEN CONDITION IS MET AND CHECKING SHOULD BE DONE

    // ENABLE THE CHECKING FLAG AFTER 3 second
    setTimeout(() => { shouldCheckCondition = true; }, 3000);
  }

My suggestion would be to add a flag - another variable that you can use to check if an operation should be done, and another function that'll toggle the flag after a certain amount of time.

// THE CHECKING FLAG
let shouldCheckCondition = true;

function animate(){
  requestAnimationFrame(animate)
  
  // CHECK THE FLAG FIRST, IF SHOULD NOT CHECK, EXIT THE FUNCTION
  if (!shouldCheckCondition) return;

  if (condition){
    // DISABLE THE CHECKING FLAG SO YOU SKIP THE SUBSEQUENT CHECKS
    shouldCheckCondition = false;

    // DO SOMETHING WHEN CONDITION IS MET AND CHECKING SHOULD BE DONE

    // ENABLE THE CHECKING FLAG AFTER 3 second
    setTimeout(() => { shouldCheckCondition = true; }, 3000);
  }
私野 2025-01-29 14:42:55

如果您想在另一种方法中内联行动,SetInterval可能无济于事。

您只需记下您上次进行检查(例如,以毫秒为单位的系统时间),然后确保下一次进行检查后的检查时间超过3000ms。

date.valueOf(); //An existing date object to milliseconds format
(new Date()).valueOf(); //current time in ms

您可以有两个var s lastcheckedtimecurrenttime

If you want to do it inline within another method, setInterval might not help.

You can just make a note of the last time you did that check (eg. system time in milliseconds) and then make sure that it is more than 3000ms after that when you next do the check.

date.valueOf(); //An existing date object to milliseconds format
(new Date()).valueOf(); //current time in ms

You can have two vars lastCheckedTime and currentTime.

初熏 2025-01-29 14:42:55

requestAnimationFrame尝试在下一个帧上运行,而不是三秒钟后。因此,如果要三秒钟,请不要使用requestAnimationFrame

function animate(){
    if (checking something){
        setTimeout(animate, 3000);
    } else {
        requestAnimationFrame(animate);
    }
}

requestAnimationFrame tries to run on the next frame, not after three seconds. So don't use requestAnimationFrame if you want three seconds.

function animate(){
    if (checking something){
        setTimeout(animate, 3000);
    } else {
        requestAnimationFrame(animate);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文