Just clearing the interval will not work, because totalSeconds will not increase. I would set a flag indicating whether time is paused or not.
This flag will simply be set when pause () or unset on resume () is called. I separated totalSeconds before the tick timeout, which will always work even when it is paused (so that we can track the time when we resume).
Thus, the checkmark function only updates the time if the clock does not pause.
function clock() { var pauseObj = new Object(); var totalSeconds = 0; var isPaused = false; var delay = setInterval(tick, 1000); function pause() { isPaused = true; } function resume() { isPaused = false; } function setTime() { var ctr; $(".icon-play").each(function(){ if( $(this).parent().hasClass('hide') ) ctr = ($(this).attr('id')).split('_'); }); $("#hour_" + ctr[1]).text(pad(Math.floor(totalSeconds/3600))); $("#min_" + ctr[1]).text(pad( Math.floor((totalSeconds/60)%60))); $("#sec_" + ctr[1]).text(pad(parseInt(totalSeconds%60))); } function tick() { ++totalSeconds; if (!isPaused) setTime(); } }
dcasadevall
source share