Created
January 28, 2019 13:47
-
-
Save sealucky7/4a21ebe3bf47f6215c8933e24663f9c5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
class Clock { | |
constructor(options) { | |
this._template = options.template; | |
} | |
start() { | |
this._render(); | |
this._timer = setInterval(() => { | |
this._render(); | |
}, 1000) | |
} | |
stop(){ | |
clearInterval(this._timer) | |
} | |
_render(){ | |
let currentDate = new Date(); | |
let hours = ('0' + currentDate.getHours()).slice(-2); | |
let minutes = ('0' + currentDate.getMinutes()).slice(-2); | |
let seconds = ('0' + currentDate.getSeconds()).slice(-2); | |
let timeString = this._template | |
.replace('h', hours) | |
.replace('m', minutes) | |
.replace('s', seconds); | |
console.log(timeString); | |
} | |
} | |
class ExtendedClock extends Clock { | |
constructor(options) { | |
super(options); | |
this._precision = options.precision; | |
} | |
start() { | |
this._render(); | |
this._timer = setInterval(() => { | |
this._render(); | |
}, this._precision) | |
} | |
} | |
let clock = new ExtendedClock({ | |
template: 'h:m:s', | |
precision: 2000, | |
}); | |
clock.start(); | |
setTimeout(() => { | |
clock.stop() | |
}, 6500); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment