如果JS某段代码执行时间过久,会影响定时器任务的执行 function bar() { console.log('bar') // bar并不会被立即打印,而是等到for循环结束之后才被打印 } function foo() { setTimeout(bar, 0); for (let i = 0; i < 5000; i++) { console.log(i) } } foo() 如果setTimeout存在嵌套,那么系统会设置最短时间间隔为4毫秒 在Chrome中嵌套调用大于等于5次,系统会判断该函数方法被阻塞了,如果定时器的调用时间间隔小于 4 毫秒,那么浏览器会将每次调用的时间间隔设置为 4 毫秒 function testSetTimeoutFor4MS () { let index = 0 let timer = null function cb () { console.timeEnd(index) if (index >= 10) { clearTimeout(timer) timer = null return false } console.time(..... 关于setTimeout的小“秘密” JavaScript