javascript - js渲染循环一旦满足条件就会中断

标签 javascript loops html canvas

如何设置在满足条件后中断的 setInterval 渲染循环?

最佳答案

您可以存储间隔 ID 并通过 clearInterval() 清除它, 例如

var timer = setInterval(myFunction, 1000);

function myFunction() {
  if(condition) {
    clearInterval(timer);
    return;
  }
  //do stuff
}

或者如果您可以在设置条件的地方调用 clearInterval(),那么下一个间隔不会运行,函数本身没有逻辑。

关于javascript - js渲染循环一旦满足条件就会中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3815066/

相关文章:

javascript - AngularJS展开和折叠问题

Python 3.6 : Looping through files in os. listdir() 并将其中一些写入文本文档

javascript - 无法获取 JSON 输出

html - R knitr - 用于小文本的 kable 表格 html 格式

javascript - 显示模态 - 每天仅显示一次

javascript - 将 `undefined` 转换为 `map<string, AnonymousModel>` 时出错

javascript - Firebase 使用相反顺序的键进行排序

java - 在数组索引处查找字符串? java

javascript - 使用 selenium 和 beautifulsoup 进行网页抓取时,通过 id、class、xpath、css 选择器查找元素不会返回任何内容

javascript - 滑动菜单不会滑动