Is setTimeout with no delay the same as executing the function instantly?

前端 未结 3 2237
夕颜
夕颜 2020-11-27 18:31

I am looking at some existing code in a web application. I saw this:

window.setTimeout(function () { ... })

Is this the same as just executing t

3条回答
  •  独厮守ぢ
    2020-11-27 19:04

    You are missing the millisecond parameter...

    setTimeout(function() { /*something*/ }, 0);
    

    The 0 sets the delay to 0 but what it actually does is to let your function "jump the queue" of the browser execution list. The browser has a bunch of things to do such as rendering objects on the page, and by calling this, your function will run as soon as the browser has some cycles.

提交回复
热议问题