Javascript-форум (https://javascript.ru/forum/)
-   Общие вопросы Javascript (https://javascript.ru/forum/misc/)
-   -   ClearInterval не срабатывает (https://javascript.ru/forum/misc/60401-clearinterval-ne-srabatyvaet.html)

m1lk1way 27.12.2015 02:56

ClearInterval не срабатывает
 
Для практики пишу свой таймер обратного отсчета. Возникла следующая проблема: не могу остановить таймер. ClearInterval не срабатывает.
Код:

function Countdowner(data){
  };

  Countdowner.prototype = {
    injectTo: function(element) {
    },
    init: function(y,m,d) {
      this.todat = new Date(y,m,d);
    },
    start: function() {
      setInterval(this.redraw.bind(this), 1000);
    },
    stop: function() {
      clearInterval(this.redraw);
    },
    redraw: function() {
      this.fromdate = Date.now();
      console.log('<--------------------------------------->');
      this.resdate = (this.todat-this.fromdate);

      this.remDays = Math.floor(this.resdate/1000/60/60/24);
      this.resdate -= this.remDays*1000*60*60*24;
      this.remHours = Math.floor(this.resdate/1000/60/60);
      this.resdate -= this.remHours*1000*60*60;
      this.remMin = Math.floor(this.resdate/1000/60);
      this.resdate -=this.remMin*1000*60;
      this.remSec = Math.floor(this.resdate/1000);

      console.log('Days:'+this.remDays+' Hours:'+this.remHours+' Minutes:'+this.remMin+' Seconds:'+this.remSec);
    }
  };

var count1 = new Countdowner();
count1.init(2016, 0, 1);
count1.start();


Decode 27.12.2015 03:03

m1lk1way,

start: function() {
  this._timerID = setInterval(this.redraw.bind(this), 1000);
},
stop: function() {
  clearInterval(this._timerID);
},

m1lk1way 27.12.2015 03:08

Decode, Спасибо и с наступающим :)

m1lk1way 27.12.2015 15:16

Снова споткнулся) При вызове injectTo element.appendChild оказывается хитрее меня, не могу понять что ему не нравится.
function Countdowner(data){

    this.self = document.createElement('div');
    this.self.classList.add('Countdowner');

    this.days = document.createElement('span');
    this.days.classList.add('days');
    this.hours = document.createElement('span');
    this.hours.classList.add('hours');
    this.minutes = document.createElement('span');
    this.minutes.classList.add('minutes');
    this.seconds = document.createElement('span');
    this.seconds.classList.add('seconds');

    this.self.appendChild(this.days);
    this.self.appendChild(this.hours);
    this.self.appendChild(this.minutes);
    this.self.appendChild(this.seconds);
  };

  Countdowner.prototype = {
    injectTo: function(element) {
      console.log(this.self); // проверим как выстроился элемент который будем вставлять в DoM
      element.appendChild(this.self); //element.appendChild is not a function(…)
    },
    init: function(y,m,d) {
      this.todat = new Date(y,m,d);
    },
    start: function() {
      clearInterval(this._timerID);
      this._timerID = setInterval(this.getDate.bind(this), 1000);
    },
    stop: function() {
      clearInterval(this._timerID);
    },
    createDom: function(getDate){
      console.log(getDate());
    },
    getDate: function() {
      this.fromdate = Date.now();
      this.resdate = (this.todat-this.fromdate);
      this.remDays = Math.floor(this.resdate/1000/60/60/24);
      this.resdate -= this.remDays*1000*60*60*24;
      this.remHours = Math.floor(this.resdate/1000/60/60);
      this.resdate -= this.remHours*1000*60*60;
      this.remMin = Math.floor(this.resdate/1000/60);
      this.resdate -=this.remMin*1000*60;
      this.remSec = Math.floor(this.resdate/1000);
      console.log('Days:'+this.remDays+' Hours:'+this.remHours+' Minutes:'+this.remMin+' Seconds:'+this.remSec);
      return([this.remDays, this.remHours, this.remMin, this.remSec]);
    }
  };

var count1 = new Countdowner();
count1.init(2016, 0, 1);
count1.start();

Decode 27.12.2015 16:32

m1lk1way, вставляется же.

count1.injectTo(document.body);

m1lk1way 27.12.2015 17:14

глупая ошибка)) спасибо :victory:


Часовой пояс GMT +3, время: 06:17.