简单的订阅发布模式

var TimeLine = function(){
	this.order = [];
}
TimeLine.prototype.add = function( t , callback ){
	this.order.push({
		timeout : t,
		callback : callback
	});
}
TimeLine.prototype.fire = function( ff ){
	for(var i = 0 , len = this.order.length ; i < this.order.length ; i++){
		var timeout = this.order[i].timeout;
		var fn = this.order[i].callback;
		timeout = Math.max( timeout - ( ff || 0 ) , 0 );
		setTimeout( fn , timeout );
	}
}

var t = new TimeLine();
t.add( 1000 , function(){
	console.log("1000");
});
t.add( 3000 , function(){
	console.log("3000");
});
t.fire( 2000 );

 

posted @ 2016-08-23 09:25  点点乐淘淘  阅读(344)  评论(0编辑  收藏  举报