diff --git a/jquery.simple.timer.js b/jquery.simple.timer.js index 3802497..8c5f290 100644 --- a/jquery.simple.timer.js +++ b/jquery.simple.timer.js @@ -37,6 +37,7 @@ var timer; var Timer = function(targetElement){ + this.startTime = new Date().getTime(); this._options = {}; this.targetElement = targetElement; return this; @@ -191,6 +192,7 @@ this.setFinalValue(this.formatTimeLeft(timeLeft), element); intervalId = setInterval((function() { + this.forwardCurrentTime(); timeLeft = endTime - this.currentTime(); // When timer has been idle and only resumed past timeout, // then we immediatelly complete the timer. @@ -210,9 +212,13 @@ element.find('.jst-hours').text('00:'); }; - Timer.prototype.currentTime = function() { - return Math.round((new Date()).getTime() / 1000); - }; + Timer.prototype.forwardCurrentTime = function () { + this.startTime += 1000; + }; + + Timer.prototype.currentTime = function () { + return Math.round(this.startTime / 1000); + }; Timer.prototype.formatTimeLeft = function(timeLeft) { @@ -259,7 +265,6 @@ element.find('.' + this._options.classNameHours).text(finalValues.pop() + ':'); }; - $.fn.startTimer = function(options) { this.TimerObject = Timer; Timer.start(options, this);