让前端飞WEB前端程序开发

用javascript实现一个事件队列

2019-01-05  本文已影响1人  gzgogo

详见github,使用方法参考index.html

function EventQueue(delayTime) {
  this._delayTime = delayTime || 20;
  this._queue = [];
}

EventQueue.prototype = {
  add: function (excute, params) {
    this._queue.push({
      excute: excute,
      params: params
    });

    this.start();
  },

  start: function () {
    if (this._delayTime <= 0) {
      this.process();
    } else {
      var self = this;
      setTimeout(function () {
        self.process();
      }, self._delayTime);
    }
  },

  process: function () {
    var item = this._queue.shift();

    if (item) {
      var self = this;
      item.excute(item.params, function () {
        self.process();
      });
    }
  }
};
上一篇 下一篇

猜你喜欢

热点阅读