asmodeus94

timer

Dec 19th, 2014
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Timer() {
  2.     this.timer = 0;
  3.     this.time = null;
  4.     this.lvl = 0;
  5.     this.initialValue = 0;
  6.     this.currentValue = 0;
  7.     this.periodCallback = null;
  8.     this.endCallback = null;
  9.     this.o = this;
  10.  
  11.     this.formatTime = function () {
  12.         var currentValueTmp = this.currentValue;
  13.         var seconds, minutes, hours, days;
  14.         var seconds1 = '';
  15.         var minutes1 = '';
  16.         var hours1 = '';
  17.         var days1 = '';
  18.         switch (this.lvl) {
  19.             case 4:
  20.                 days = Math.floor(currentValueTmp / 86400);
  21.                 currentValueTmp = currentValueTmp - days * 86400;
  22.                 days1 = days + '';
  23.                 if (days1.length < 2) days1 = '0' + days1;
  24.             case 3:
  25.                 hours = Math.floor(currentValueTmp / 3600);
  26.                 currentValueTmp = currentValueTmp - hours * 3600;
  27.                 hours1 = hours + '';
  28.                 if (hours1.length < 2) hours1 = '0' + hours1;
  29.             case 2:
  30.                 minutes = Math.floor(currentValueTmp / 60);
  31.                 currentValueTmp = currentValueTmp - minutes * 60;
  32.                 minutes1 = minutes + '';
  33.                 if (minutes1.length < 2) minutes1 = '0' + minutes1;
  34.             case 1:
  35.                 seconds = currentValueTmp;
  36.                 seconds1 = seconds + '';
  37.                 if (seconds1.length < 2) seconds1 = '0' + seconds1;
  38.         }
  39.  
  40.         this.time = {
  41.             days: days1,
  42.             hours: hours1,
  43.             minutes: minutes1,
  44.             seconds: seconds1
  45.         };
  46.     };
  47.  
  48.     this.subtractTimer = function () {
  49.         if (this.currentValue > 0) {
  50.             this.formatTime();
  51.             if (typeof this.periodCallback === 'function') {
  52.                 this.periodCallback(this.time);
  53.             }
  54.         } else {
  55.             if (typeof this.endCallback === 'function') {
  56.                 this.endCallback();
  57.             }
  58.             this.time = {
  59.                 days: '00',
  60.                 hours: '00',
  61.                 minutes: '00',
  62.                 seconds: '00'
  63.             };
  64.         }
  65.         this.currentValue--;
  66.     };
  67.  
  68.     this.kill = function () {
  69.         clearInterval(this.timer);
  70.     };
  71.  
  72.     this.setPeriodCallback = function (callback) {
  73.         this.periodCallback = callback;
  74.         return this;
  75.     };
  76.  
  77.     this.setEndCallback = function (callback) {
  78.         this.endCallback = callback;
  79.         return this;
  80.     };
  81.  
  82.     this.setDisplay = function (lvl) {
  83.         this.lvl = lvl;
  84.         return this;
  85.     };
  86.  
  87.     this.start = function (value) {
  88.         this.initialValue = parseInt(value);
  89.         var ob = this.o;
  90.         clearInterval(this.timer);
  91.         if (parseInt(this.lvl) === 0) {
  92.             this.lvl = 1;
  93.             if (this.initialValue > 60)
  94.                 this.lvl = 2;
  95.             if (this.initialValue > 3600)
  96.                 this.lvl = 3;
  97.             if (this.initialValue > 86400)
  98.                 this.lvl = 4;
  99.         }
  100.         this.currentValue = this.initialValue;
  101.         this.timer = setInterval(function () {
  102.             if (ob.currentValue >= 0) {
  103.                 ob.subtractTimer();
  104.             }
  105.             else {
  106.                 ob.kill();
  107.             }
  108.         }, 1000);
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment