`
<html>
<head>
<base target="_top">
</head>
<body>
<h2>you have <span id="timer"> </span> seconds left</h2>
<input type="button" id="next" value="First Question" >
<script>
const nextbtn=document.getElementById("next");
nextbtn.addEventListener("click",timedis());
var timecount=60;
function timedis(){
setInterval(function(){
document.getElementById("timer").innerHTML=timecount;
timecount-=1;
},1000);
</script>
</body>
</html>
Asked
Active
Viewed 10 times
0
-
1`timedis()` calls the function immediately. Remove the parenthesis. – Jun 02 '21 at 14:14
-
Another duplicate: https://stackoverflow.com/questions/13286233/pass-a-javascript-function-as-parameter – Jun 02 '21 at 14:17