javascript 实现一个简单异步的队列

jQ中动画animate的队列实现

决定模仿一个:

 

function Queue(){
	this.a = [];
	this.t = null;
}
Queue.prototype = {
	queue:function(s){
		var self = this;
		this.a.push(s);
		this.hold();
		return this;
	},
	hold:function(){
		var self = this;
		clearTimeout(this.t);
		this.t = setTimeout(function(){
			console.log("队列开始 start! ",self.a);
			self.dequeue();
		},0);
	},
	dequeue:function(){
		var s = this.a.shift(),self = this;
		if(s){
			console.log("s:"+s);
			setTimeout(function(){
				console.log("end:"+s);
				self.dequeue();
			},s);
		}
	}
};




var a = new Queue().queue(500).queue(200).queue(400).queue(1500).queue(300).queue(2000);

  

 

 

 

posted @ 2011-11-26 21:00 tangoboy 阅读(...) 评论(...) 编辑 收藏