Advertisement
Guest User

Untitled

a guest
Feb 21st, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Created by bafnee on 2/20/17.
  3.  */
  4. function Clock(options) {
  5.     "use strict";
  6.     this._timer;
  7.     this._template = options.template;
  8. }
  9. Clock.prototype._render = function(){
  10.     "use strict";
  11.         var date = new Date();
  12.  
  13.         var hours = date.getHours();
  14.         if (hours < 10) hours = '0' + hours;
  15.  
  16.         var min = date.getMinutes();
  17.         if (min < 10) min = '0' + min;
  18.  
  19.         var sec = date.getSeconds();
  20.         if (sec < 10) sec = '0' + sec;
  21.  
  22.         var output = this._template.replace('h', hours).replace('m', min).replace('s', sec);
  23.  
  24.         console.log(output);
  25. }
  26. Clock.prototype.stop = function() {
  27.     "use strict";
  28.     clearInterval(this._timer);
  29. }
  30. Clock.prototype.start = function() {
  31.     "use strict";
  32.     this._render();
  33.     var self = this;
  34.     this._timer = setInterval(function(){
  35.         self._render();
  36.     },1000);
  37. }
  38.  
  39.  
  40. // ClockExtended
  41. function ClockExtended(options) {
  42.     "use strict";
  43.     Clock.apply(this,arguments);
  44.     this._interval = options.interval || 1000;
  45. }
  46. ClockExtended.prototype = Object.create(Clock);
  47. ClockExtended.prototype.constructor = ClockExtended;
  48.  
  49. ClockExtended.prototype.start = function() {
  50.     "use strict";
  51.     Clock.prototype._render.apply(this);
  52.     var self = this;
  53.  
  54.     this._timer = setInterval(function(){
  55.         Clock.prototype._render.apply(self);
  56.     },self._interval);
  57. }
  58.  
  59. // run the clock
  60. var clock = new ClockExtended({
  61.     template: 'h:m:s',
  62.     interval: 2000,
  63. });
  64. clock.start();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement