I'm writing a timer function that uses setInterval
within a revealing module pattern. I can get the timer to start fine, but I'm having trouble stopping the interval.
var timer = (function() {
var startTimer = function() {
setInterval(function() {
// Do Stuff
}, 1000);
};
var stopTimer = function() {
clearInterval(timer.startTimer());
};
return {
startTimer: startTimer,
stopTimer: stopTimer
};
})();
timer.startTimer(); // Works!
timer.stopTimer(); // Doesn't Work!
Any ideas?
var timer = (function() {
var timerRef = null;
var startTimer = function() {
timerRef = setInterval(function() {
// Do Stuff
}, 1000);
};
var stopTimer = function() {
clearInterval(timerRef);
};
return {
startTimer: startTimer,
stopTimer: stopTimer
};
})();