javascript模拟sleep
当我们在一个js的方法里面,执行一部分代码后,需要让js暂停一断时间,然后再继续向下运行的时候,或者你会想到java的线程sleep(),但js 里面没有暂停的函数,所以要自己实现一个,具体方法看下面的:
使用方法很简单:
- //js暂停函数
- function
Pause(obj,iMinSecond){ -
if (window.eventList== null)window.eventList= newArray(); -
var ind=-1; -
for (var 0;i<window.eventList.length;i++){i= -
if (window.eventList[i]== null){ -
window.eventList[i]=obj; -
ind=i; -
break; -
} -
} -
if (ind==- 1){ -
ind=window.eventList.length; -
window.eventList[ind]=obj; -
} -
setTimeout("GoOn(" + ")",iMinSecond);ind + - }
-
- //js继续函数
- function
GoOn(ind){ -
var obj=window.eventList[ind]; -
window.eventList[ind]=null; -
if (obj.NextStep) obj.NextStep(); -
else obj(); - }
使用方法很简单:
- function
testJsStop(){ -
alert("1"); -
Pause(this,3000); -
this.NextStep=function(){ -
alert("2"); -
} - }

浙公网安备 33010602011771号