First try returning your interval to a function:
function startTimer(duration, display) { var timer = duration, 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; display.text(minutes + ":" + seconds); if (--timer < 0) { timer = duration; } }, 1000); return timeInterval; }
Then, by calling this function, assign it to a variable so that you can clear it later:
var countTimeInterval = startTimer(); clearInterval(countTimeInterval);
source share