简单的订阅发布模式

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 );
原文地址:https://www.cnblogs.com/diantao/p/5797998.html