en.javascript.info/1-js/9-object-inheritance/10-class-inheritance/3-clock-class/solution.view/clock.js
Ilya Kantor b0976b5253 up
2016-11-14 23:41:18 +03:00

32 lines
No EOL
673 B
JavaScript

function Clock(options) {
this._template = options.template;
}
Clock.prototype._render = function render() {
var date = new Date();
var hours = date.getHours();
if (hours < 10) hours = '0' + hours;
var min = date.getMinutes();
if (min < 10) min = '0' + min;
var sec = date.getSeconds();
if (sec < 10) sec = '0' + sec;
var output = this._template.replace('h', hours).replace('m', min).replace('s', sec);
console.log(output);
};
Clock.prototype.stop = function() {
clearInterval(this._timer);
};
Clock.prototype.start = function() {
this._render();
var self = this;
this._timer = setInterval(function() {
self._render();
}, 1000);
};