Why does not clear clearInterval in the moveTo function while everything works on the pause button.

var timerid = 0, counterid = 0; function playPages() { var sec = 3; timerid = setInterval(counterid = moveTo(sec), sec * 1000); } function moveTo(sec) { clearInterval(counterid) document.getElementById('backcounter').innerText = sec; return setInterval(changeBackcounter, 1000); } function changeBackcounter() { var temp = document.getElementById('backcounter').innerText; document.getElementById('backcounter').innerText = --temp; } function pausePages() { clearInterval(counterid); } document.getElementById('play').addEventListener("click", playPages); document.getElementById('pause').addEventListener("click", pausePages); 
  • So you set the timerid identifier to the timerid and try to stop the counterid - Vadim Leshkevich
  • I understand two and more setinterval set at the same time is not possible, and yes, I want to stop the counterid so that the counter starts from the beginning, and the first interval should continue to work - Yugr
  • at the same time you can ask. each call to setInterval, returns the id of the started timer. What happens in the code of the moveTo / playPages method is not clear. - Grundy

2 answers 2

Perhaps you need to change the function PlayPages

 function playPages() { var sec = 3; timerid = setInterval(function(){ counterid = moveTo(sec) }, sec * 1000); } 
  • so also tried, does not help - Yugr

Colleagues thank you all who tried to help, everything is correct except for this

 timerid = setInterval(counterid = moveTo(sec), sec * 1000); 

the second argument must be a variable or a number, the expression is not accepted

  • where is the second argument in this line? - Grundy
  • second argument sec * 1000 - Yugr
  • sec * 1000 - quite a number returns to itself. Hence the problem is clearly not the case. - Grundy
  • What was this line supposed to do? since moveTo returns a number, not a function. What was the error in the browser console? - Grundy