2

I got this interval function which adds + 1 every second to this element, and a button which stops it with clearInterval(), but i want to "stop" and "resume" the interval instead of "clear" and restarting it. For example if the interval was stopped with 700 miliseconds, so i want to resume it for runnig from these 700 miliseconds and not from 0.

How can i do that?.

var testingB = $('#testing');

var testingBox = $('.text3');

var counter = 0;

var checker = null;

setInterval( function () { testingBox.text(counter); },1);
var id = setInterval( function () { counter++; },1000);

function adder() {
 if (checker === null ) {
  clearInterval (id);
  checker = true;
  testingB.text('Keep Counting');
 }
 else {
  id = setInterval( function () { counter++; },1000);
  checker = null;
  testingB.text('Stop Couting');
 };
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<button type="button" id="testing" onclick="adder()"> Stop Counting </button>
<p class="text3"> </p>
  • wrap the setInterval call in a setTimeout with the remaining # of ms. you can subtract two calls to `Date.now()` to get the ms difference. – dandavis Jan 26 '18 at 23:45
  • Possible duplicate of [Pause and resume setInterval](https://stackoverflow.com/questions/24724852/pause-and-resume-setinterval) – showdev Jan 26 '18 at 23:48
  • So I just ran the code snippet above and it already seems to be doing what you want. – Cogwizzle Jan 27 '18 at 04:29

0 Answers0