Показать сообщение отдельно
  #7 (permalink)  
Старый 11.04.2016, 13:52
Аватар для nerv_
junior
Отправить личное сообщение для nerv_ Посмотреть профиль Найти все сообщения от nerv_
 
Регистрация: 29.11.2011
Сообщений: 3,924

woojin, правильный вариант того, что ты хочешь сделать, выглядит как-то так

function Timer(selector) {
  this.id = null;
  this.element = document.querySelector(selector);
  this.tick = this.tick.bind(this);
  this.reset();
}

Timer.prototype.start = function() {
  this.reset();
  this.id = setTimeout(this.tick, 1000);
};
Timer.prototype.tick = function() {
  this.print();
};
Timer.prototype.reset = function () {
  this.timestamp = Date.now();
};
Timer.prototype.stop = function () {
  this.reset();
  clearTimeout(this.this.id);
};
Timer.prototype.print = function () {
  var diff = Date.now() - this.timestamp; // ms
  var seconds = Math.ceil(diff / 1000);
  this.element.innerHTML = "last answer: " + seconds + " sec";
};

// usage

var timer = new Timer("#answerTimer");
timer.start();
__________________
Чебурашка стал символом олимпийских игр. А чего достиг ты?
Тишина - самый громкий звук

Последний раз редактировалось nerv_, 11.04.2016 в 13:55.
Ответить с цитированием