js任务链

var TASK_SYNC = 0;
var TASK_ASYNC = 1;

function Animation(){
  this.taskQueue = [];
}

Animation.prototype._add = function(task,type){
  this.taskQueue.push({
    taskFn: task,
    type: type
  })
  return this;
}

Animation.prototype._runTask = function(){
  if(this.taskQueue.length){
    for(key in this.taskQueue){
      this.taskQueue[key].taskFn();
    }
  }
}

Animation.prototype.start = function(){
  this._runTask();
}

Animation.prototype.say = function(){
  var taskFn = function(){
    console.log('say fn');
  }
  var type = TASK_SYNC;
  
  return this._add(taskFn,type)
}

Animation.prototype.run = function(){
  var taskFn = function(){
    console.log('run fn');
  }
  var type = TASK_SYNC;
  
  return this._add(taskFn,type)
}

var animation = new Animation();

animation.say().run().start();

 

posted @ 2017-02-16 16:29  sunbey80  阅读(235)  评论(0编辑  收藏  举报