var i = 0;this.setInterval(function() {if(!$('#counter').hasClass('pauseInterval')) { //only run if it hasn't got this class 'pauseInterval'console.log('Counting...');$('#counter').html(i++); //just for explaining and showing} else {console.log('Stopped counting');}}, 500);
/* In this example, I'm adding a class on mouseover and remove it again on mouseleave. You can of course do pretty much whatever you like */$('#counter').hover(function() { //mouse enter$(this).addClass('pauseInterval');},function() { //mouse leave$(this).removeClass('pauseInterval');});
/* Other example */$('#pauseInterval').click(function() {$('#counter').toggleClass('pauseInterval');});
<!-- you'll need jQuery for this. If you really want a vanilla version, ask --><script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p id="counter"> </p><button id="pauseInterval">Pause</button></p>
// Timer with 1000ms (1 second) base interval resolution.const timer = new TaskTimer(1000);
// Add task(s) based on tick intervals.timer.add({id: 'job1', // unique id of the tasktickInterval: 5, // run every 5 ticks (5 x interval = 5000 ms)totalRuns: 10, // run 10 times only. (omit for unlimited times)callback(task) {// code to be executed on each runconsole.log(task.name + ' task has run ' + task.currentRuns + ' times.');// stop the timer anytime you likeif (someCondition()) timer.stop();// or simply remove this task if you have othersif (someCondition()) timer.remove(task.id);}});
// Start the timertimer.start();