Whenever I am loading the page, the function is starting automatically, without even clicking on the button.
btn1 = document.getElementById('btn1');
btn1.addEventListener("click", getTime(1,0));
I have taken the button id to a new variable, and then added (addEventlistener) on click, but whenever I am reloading the page, the function getTime() is automatically started.
function getTime(x,y){
text = document.getElementById('timer-text');
text.innerHTML = 'Started';
var min = x;
var sec = y;
total_time = (min*60) + sec;
setInterval(()=>{
timerText = document.getElementById('timer-text');
if(total_time<=0){
timerText.innerHTML = 'TIME OUT!';
clearInterval();
return;
}
let minText = Math.floor(total_time/60);
if(minText<10){
minText = '0' + minText;
}
let secText = (total_time%60);
if(secText<10){
secText = '0'+secText;
}
timerText.innerHTML = minText +" : "+ secText;
total_time = total_time - 1;
}, 1000)
}