I have these 2 functions in a library file. One starts the countdown timer and the other stops it. To be able to stop it I needed the reference for the timeInterval hence had to make it global. Sure this is not the proper way to do. How should this be handled if these functions are in a separate utility file? Thanks.
var timeInterval;
exports.stopTimer = function() {
clearInterval(timeInterval);
};
exports.startTimer = function(duration, interval, displayElem, cbFinished, cbInterval) {
var timer = duration * 60,
interval = interval * 60,
minutes,
seconds;
timeInterval = setInterval(function() {
minutes = parseInt(timer / 60, 10);
seconds = parseInt(timer % 60, 10);
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
displayElem.text = minutes + ":" + seconds;
if (--timer < 0) {
clearInterval(timeInterval);
if (cbFinished) {
cbFinished();
}
}
if (timer == interval) {
if (cbInterval) {
cbInterval();
}
}
}, 1000);
};
