Ya tengo este código en javascript
<script type="text/javascript"> var count = 20; // Timer var redirect = "https://www.instagram.com/"; // Target URL function countDown() { var timer = document.getElementById("timer"); // Timer ID if (count > 0) { count--; timer.innerHTML = "przekieruje cię za " + count + " sekund."; // Timer Message setTimeout("countDown()", 1000); } else { window.location.href = redirect; } } </script>Me gustaría colocar el botón con pausa en mi sitio html; funcionará como 'si presiono el botón mientras se ejecuta la cuenta regresiva, se detendrá, pero si hago clic nuevamente, se reanudará'. Intenté poner clearInterval en el código pero no funcionó. Soy realmente un aficionado cuando se trata de javascript.
Nada especial
(async()=>{ const timer = document.getElementById('timer'); let timerId; let tf = false; let k = 7; const plusplus = async()=>{ timer.innerHTML = k + ' dancing cows left'; if(k < 1)clearInterval(timerId); else k--; }; plusplus(); timerId = setInterval(plusplus, 1000); document.getElementById('button').addEventListener('click', ()=>{ if(!tf)clearInterval(timerId); else timerId = setInterval(plusplus, 1000); tf = !tf; }); })(); <button id="button">stop / resume</button> <div id="timer"></div>