Does this pattern:
setTimeout(function(){ // do stuff }, 0);
Actually return control to the UI from within a loop? When are you supposed to use it? Does it work equally well in all browsers?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It runs code asynchronously (not in parallel though). The delay is usually changed to a minimum of 10ms, but that doesn’t matter.
The main use for this trick is to avoid limit of call stack depth. If you risk reaching limit (walk deep tree structure and plan to do a lot of work on leafs), you can use timeout to start function with a new, empty call stack.
You can also use it to avoid blocking current thread. For example if you don’t want
<script>element to delay loading of a page: