javascript 延迟执行

	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。

posted @ 2013-01-15 14:51  ascrat  阅读(287)  评论(0编辑  收藏  举报