Does SetInterval run things on a seperate thread? How does the method work?

我的未来我决定 提交于 2021-01-27 04:09:08

问题


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.

Thanks.


回答1:


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) { }

The infinite loop will prevent the function from running, because the JS runtime is stuck in the loop.



来源:https://stackoverflow.com/questions/29973224/does-setinterval-run-things-on-a-seperate-thread-how-does-the-method-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!