我无法理解 setInterval
和 clearInterval
在 JavaScript 中的工作方式。
我已经阅读了关于这两个功能的不同文章,但我仍然感到困惑。
考虑到 JavaScript 是单线程
,那么当我们调用 test=setInterval(function(){}, t)
时,我们将安排我们的函数每隔一段时间执行一次t
毫秒。除非我们 clearInterval(test)
。
- 这是什么意思?
clearInterval
是否意味着我们取消
已经在JavaScript队列中的setInterval
? - 如果我们
setInterval
多次而clearInterval
仅一次会怎样? - 我们可以在
setInterval
中clearInterval
吗? - JavaScript 如何处理预定进程?
最佳答案
What does that mean? Does it mean that by clearInterval we cancle the setInterval which is already in the queue of JavaScript?
是的。
What would happen if we setInterval several times but clearInterval only once?
每个 setInterval
都会返回一个句柄,您可以将其与 clearInterval
一起使用以停止该特定计时器。每个计时器都是单独处理的。
Can we clearInterval in setInterval?
是的。
How JavaScript handles the scheduled process?
与任何其他事件非常相似。每隔一定时间触发一个事件,事件处理程序调用您指定的回调函数。如果事件触发时某些脚本正在运行,则当控件返回浏览器时将处理该事件。
关于javascript - 作为单线程语言的 JavaScript 中的 setInterval 和 clearInterval,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29612512/