SetInterval 是否在单独的线程上运行?该方法是如何工作的? [英] Does SetInterval run things on a seperate thread? How does the method work?
问题描述
我环顾四周,试图了解 SetInterval
是如何使用的,但只找到了如何使用它.我已经知道它的功能,我只是想知道当 JS 不支持线程时它如何能够在单独的线程上运行某些东西(至少我是这么读的).
I've looked around trying to understand how SetInterval
but only found how to use it. I already know it's functionality, I'm just curious about how it's able to run something on a separate thread when JS doesn't support threading(at least that's what I read).
我希望我正确地表述了这个问题.
I hope I formulated the question properly.
谢谢.
推荐答案
setInterval
不在不同的线程上运行任何东西.它会安排一些东西在特定时间运行前提是当时 JS 运行时是空闲的.
setInterval
does not run anything on a different thread. It schedules something to run at certain times provided the JS runtime is idle at that time.
您可以通过以下方式尝试这种行为:
You can try out this behavior with something like this:
setInterval(function(){ alert("Hello"); }, 1000);
while (true) { }
无限循环会阻止函数
运行,因为JS运行时卡在循环中.
The infinite loop will prevent the function
from running, because the JS runtime is stuck in the loop.
这篇关于SetInterval 是否在单独的线程上运行?该方法是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!