Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Created by bafnee on 2/20/17.
- */
- function Clock(options) {
- "use strict";
- this._timer;
- this._template = options.template;
- }
- Clock.prototype._render = function(){
- "use strict";
- 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() {
- "use strict";
- clearInterval(this._timer);
- }
- Clock.prototype.start = function() {
- "use strict";
- this._render();
- var self = this;
- this._timer = setInterval(function(){
- self._render();
- },1000);
- }
- // ClockExtended
- function ClockExtended(options) {
- "use strict";
- Clock.apply(this,arguments);
- this._interval = options.interval || 1000;
- }
- ClockExtended.prototype = Object.create(Clock);
- ClockExtended.prototype.constructor = ClockExtended;
- ClockExtended.prototype.start = function() {
- "use strict";
- Clock.prototype._render.apply(this);
- var self = this;
- this._timer = setInterval(function(){
- Clock.prototype._render.apply(self);
- },self._interval);
- }
- // run the clock
- var clock = new ClockExtended({
- template: 'h:m:s',
- interval: 2000,
- });
- clock.start();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement