LINUX.ORG.RU

История изменений

Исправление firkax, (текущая версия) :

Как это записать покрасивее?

    function intervalRolling(arr, callback, pause, final_cb) {
      this.arr = arr, this.cb = callback, this.pause = pause, this.final_cb = final_cb, this.i = 0;
      this.iId = setInterval((function() {
        this.cb(this.arr[this.i]);
        this.i++;
        if(this.i >= this.arr.length) {
          clearInterval(this.iId); this.iId = null;
          if(this.final_cb) this.final_cb();
        }
      }).bind(this), pause);
    }
    intervalRolling.prototype.stop = function() {
      if(this.iId!=null) { clearInterval(this.iId); this.iId = null; }
    }

    // пример использования
    function getNext(item) {
      console.log(item);
    }
    array1 = [ 'a1', 'a2', 'a3' ];
    xxx = new intervalRolling(array1, getNext, 1000);
// или так:    xxx = new intervalRolling(array1, getNext, 1000, function() { console.log('done'); });

// отмена: xxx.stop();

Исходная версия firkax, :

Как это записать покрасивее?

    function intervalRolling(arr, callback, pause, final_cb) {
      this.arr = arr, this.cb = callback, this.pause = pause, this.final_cb = final_cb, this.i = 0;
      this.iId = setInterval((function() {
        this.cb(this.arr[this.i]);
        this.i++;
        if(this.i >= this.arr.length) {
          clearInterval(this.iId); this.iId = null;
          if(this.final_cb) this.final_cb();
        }
      }).bind(this), pause);
    }
    intervalRolling.prototype.stop = function() {
      if(this.iId!=null) { clearInterval(this.iId); this.iId = null; }
    }

    // пример использования
    function getNext(item) {
      console.log(item);
    }
    array1 = [ 'a1', 'a2', 'a3' ];
    xxx = new intervalRolling(array1, getNext, 1000);
// или так:    xxx = new intervalRolling(array1, getNext, 1000, function() { console.log('done'); });