简体   繁体   中英

i want to stop javascript countdown timer with onclick event

i need help, i make a javascript function for countdown timer and here is code:

<script>

function start_minute() {
let startingMinutes = 20;


let time = startingMinutes * 60;

  const countDownEl = document.getElementById('countdown');

  setInterval(updateCountdown, 1000);

  function updateCountdown() {
    const minutes = Math.floor(time / 60);
    let seconds = time % 60;

    seconds = seconds < 10 ? '0' + seconds : seconds;

    countDownEl.innerHTML = `${minutes} : ${seconds}`;
    if (time > 0) {
    time--; 
    } else {
      countDownEl.innerHTML = `IT'S OVER !`
    }
    
  }
}    
}
The clearIntervall wont work directly in an input on click i need to put stopfunction in this for example

 let intervalId = null; function start_minute() { let startingMinutes = 20; let time = startingMinutes * 60; const countDownEl = document.getElementById('countdown'); intervalId = setInterval(updateCountdown, 1000); function updateCountdown() { const minutes = Math.floor(time / 60); let seconds = time % 60; seconds = seconds < 10? '0' + seconds: seconds; countDownEl.innerHTML = `${minutes}: ${seconds}`; if (time > 0) { time--; } else { countDownEl.innerHTML = `IT'S OVER;`. } } } function stop_minte() { clearInterval(intervalId) } document.getElementById('start'),addEventListener('click'; start_minute). document.getElementById('stop'),addEventListener('click'; stop_minte);
 <div id="countdown">time</div> <button id="start">start</button> <button id="stop">stop</button>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM