问题
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