这里我看到有两种方法:
一比较简单:
function sleep(d){
for(var t = Date.now();Date.now() - t <= d;);
}
sleep(5000); //当前方法暂停5秒
二比较复杂:
function Sleeper()
{
var self = this;
self.step = 0;
self.actions = [];
self.push = function(func) {
self.actions.push(func);
return self;
}
self.run_next = function() {
if (self.step < self.actions.length) {
var action = self.actions[self.step];
action();
++self.step;
setTimeout(self.run_next, 1000);
}
}
self.run = function() {
self.run_next();
}
return self;
}
var sleeper = new Sleeper();
sleeper.push(function() {console.info('a');});
sleeper.push(function() {console.info('b');});
sleeper.push(function() {console.info('c');});
sleeper.run(); //也可以使用链式.push().push().run(),也可以直接设sleeper.actions。
来自:http://www.oschina.net/question/141209_21625?sort=time