This commit is contained in:
Ilya Kantor 2016-11-14 23:41:18 +03:00
parent f99574f53b
commit b0976b5253
153 changed files with 590 additions and 533 deletions

View file

@ -0,0 +1,32 @@
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);
};

View file

@ -0,0 +1,14 @@
function ExtendedClock(options) {
Clock.apply(this, arguments);
this._precision = +options.precision || 1000;
}
ExtendedClock.prototype = Object.create(Clock.prototype);
ExtendedClock.prototype.start = function() {
this._render();
var self = this;
this._timer = setInterval(function() {
self._render();
}, this._precision);
};

View file

@ -0,0 +1,26 @@
<!DOCTYPE HTML>
<html>
<head>
<title>Часики в консоли</title>
<meta charset="utf-8">
</head>
<body>
<script src="clock.js"></script>
<script src="extended-clock.js"></script>
<script>
var lowResolutionClock = new ExtendedClock({
template: 'h:m:s',
precision: 10000
});
lowResolutionClock.start();
</script>
</body>
</html>