Stop setInterval call in JavaScript

前端 未结 16 1813
星月不相逢
星月不相逢 2020-11-21 05:00

I am using setInterval(fname, 10000); to call a function every 10 seconds in JavaScript. Is it possible to stop calling it on some event?

I want the us

16条回答
  •  终归单人心
    2020-11-21 05:54

    This is how I used clearInterval() method to stop the timer after 10 seconds.

    function startCountDown() {
      var countdownNumberEl = document.getElementById('countdown-number');
      var countdown = 10;
      const interval = setInterval(() => {
        countdown = --countdown <= 0 ? 10 : countdown;
        countdownNumberEl.textContent = countdown;
        if (countdown == 1) {
          clearInterval(interval);
        }
      }, 1000)
    }
    
      
        
      
    

提交回复
热议问题