What is the difference between setTimeout(fn, 0) and setTimeout(fn, 1)?

后端 未结 6 2010
执念已碎
执念已碎 2020-11-28 07:05

The jQuery source features uses of setTimeout with both 0 and 1 as second argument. I\'m under the impression that they both mean &quo

6条回答
  •  -上瘾入骨i
    2020-11-28 07:29

    For reasons why setTimeout(fn, 0) or setTimeout(fn, 1) is needed, check out Why is setTimeout(fn, 0) sometimes useful?.

    In essence, it means that this method is not very urgent to execute compared to other browser tasks like page rendering. Moreover, the JavaScript code will run after the waiting tasks are over.

    Practical wise, there is no difference between using 0 or 1. This is just programmer's choice. Ideally the number chosen by coders is below 4 which may be due to the reason pointed out by Amaan.

    BTW, for basic information on JavaScript timers, refer to http://ejohn.org/blog/how-javascript-timers-work/

提交回复
热议问题