Already i got this code in 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>
I would like to place button with pause in my html site - it will works like 'if i press the button while countdown is running it will be stopped, but if i click again it will resume'. I've tried to put clearInterval in code but it didn't work. I'm really an amateur when it comes to javascript.
Nothing special
(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>