var lastIndex = 0; var dvTest; var timesTotal = 200000; var lastDateTime = new Date().getTime(); window.onload = function(){ dvTest = document.getElementById("test"); TimerUpdater(); } function TimerUpdater(){ var last = new Date().getTime(); while(lastIndex < timesTotal){ dvTest.innerHTML = lastIndex; var now = new Date().getTime(); var t = now - last; lastIndex++; if(t > 100) break; } if(lastIndex >= timesTotal){ dvTest.innerHTML = new Date().getTime() - lastDateTime; return; } setTimeout(TimerUpdater, 50); }
javascript 分段执行, 当执行时间超过 100ms 延迟 50ms。