Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function Timer() {
- this.timer = 0;
- this.time = null;
- this.lvl = 0;
- this.initialValue = 0;
- this.currentValue = 0;
- this.periodCallback = null;
- this.endCallback = null;
- this.o = this;
- this.formatTime = function () {
- var currentValueTmp = this.currentValue;
- var seconds, minutes, hours, days;
- var seconds1 = '';
- var minutes1 = '';
- var hours1 = '';
- var days1 = '';
- switch (this.lvl) {
- case 4:
- days = Math.floor(currentValueTmp / 86400);
- currentValueTmp = currentValueTmp - days * 86400;
- days1 = days + '';
- if (days1.length < 2) days1 = '0' + days1;
- case 3:
- hours = Math.floor(currentValueTmp / 3600);
- currentValueTmp = currentValueTmp - hours * 3600;
- hours1 = hours + '';
- if (hours1.length < 2) hours1 = '0' + hours1;
- case 2:
- minutes = Math.floor(currentValueTmp / 60);
- currentValueTmp = currentValueTmp - minutes * 60;
- minutes1 = minutes + '';
- if (minutes1.length < 2) minutes1 = '0' + minutes1;
- case 1:
- seconds = currentValueTmp;
- seconds1 = seconds + '';
- if (seconds1.length < 2) seconds1 = '0' + seconds1;
- }
- this.time = {
- days: days1,
- hours: hours1,
- minutes: minutes1,
- seconds: seconds1
- };
- };
- this.subtractTimer = function () {
- if (this.currentValue > 0) {
- this.formatTime();
- if (typeof this.periodCallback === 'function') {
- this.periodCallback(this.time);
- }
- } else {
- if (typeof this.endCallback === 'function') {
- this.endCallback();
- }
- this.time = {
- days: '00',
- hours: '00',
- minutes: '00',
- seconds: '00'
- };
- }
- this.currentValue--;
- };
- this.kill = function () {
- clearInterval(this.timer);
- };
- this.setPeriodCallback = function (callback) {
- this.periodCallback = callback;
- return this;
- };
- this.setEndCallback = function (callback) {
- this.endCallback = callback;
- return this;
- };
- this.setDisplay = function (lvl) {
- this.lvl = lvl;
- return this;
- };
- this.start = function (value) {
- this.initialValue = parseInt(value);
- var ob = this.o;
- clearInterval(this.timer);
- if (parseInt(this.lvl) === 0) {
- this.lvl = 1;
- if (this.initialValue > 60)
- this.lvl = 2;
- if (this.initialValue > 3600)
- this.lvl = 3;
- if (this.initialValue > 86400)
- this.lvl = 4;
- }
- this.currentValue = this.initialValue;
- this.timer = setInterval(function () {
- if (ob.currentValue >= 0) {
- ob.subtractTimer();
- }
- else {
- ob.kill();
- }
- }, 1000);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment