I am trying to create a timer widget that can be initialized with a number of seconds and then reset at any time by making a new call with a new 'number of seconds' to countdown from. So far I have this which creates the timer fine and counts down.
What I am trying to do is call the _destroy method first inside the _create method to remove the timer that is already on the element and also use window.clearInterval to stop the timer being re-added. this.interval is set in the _start method and then referenced in the _destroy method but I think the problem lies with the scope of this.interval.
This is what I have so far, but my _destroy method does not seem to be clearing the interval and I can't figure out why.
$.widget('time.countdown', {
options : {
remaining : (2 * 24 * 60 * 60 * 1000),
updatefreq: 1000
},
_create : function () {
'use strict';
this._destroy();
},
_start: function () {
'use strict';
var secs = this.options.remaining;
this.interval = window.setInterval((function (elem, secs) {
return function () {
secs -= 1;
var days = Math.floor(secs / (24 * 60 * 60)),
div_hr = secs % (24 * 60 * 60),
hours = Math.floor(div_hr / (60 * 60)),
div_min = secs % (60 * 60),
minutes = Math.floor(div_min / 60),
div_secs = div_min % 60,
seconds = Math.ceil(div_secs),
time_parts = {
"d": days,
"h": hours,
"m": minutes,
"s": seconds
},
tstring = '',
c;
for (c in time_parts) {
if (time_parts.hasOwnProperty(c)) {
tstring += time_parts[c] + c + ' ';
}
}
elem.html(tstring);
};
}(this.element, secs)), this.options.updatefreq);
},
_destroy: function() {
'use strict';
if (this.interval !== undefined) {
window.clearInterval(this.interval);
}
this.element.html('');
}
});
Can anyone shed some light on this?
Something strange with your code is that there is no call to the "private"
_start()
function, but nvm.You should add a public
reset()
function to :remaining
value,_start()
functionYou should then call this
reset()
function when needed. Have a look in this code and explanations below :Note that the
_create()
function is called only once, at creation time. So if you apply the plugin many times on the same element with no args like$('#timer').countdown();
then the_start()
will be called only once (on first call).You can now reset the countdown with a new value in differents ways :