admin管理员组文章数量:1398831
New to coding. Created a timer that I would like to start when "gameStart" button is pushed, instead of on page load. Not sure where to insert my "onclick". Thank you
<div id= "startButtons">
<button id= "gameStart">Get Going!</button>
<script>
var timeleft = 15;
var downloadTimer = setInterval(function function1(){
document.getElementById("countdown").innerHTML = timeleft +
" "+"seconds remaining";
timeleft -= 1;
if(timeleft <= 0){
clearInterval(downloadTimer);
document.getElementById("countdown").innerHTML = "Time is up!"
}
}, 1000);
console.log(countdown);
</script>
New to coding. Created a timer that I would like to start when "gameStart" button is pushed, instead of on page load. Not sure where to insert my "onclick". Thank you
<div id= "startButtons">
<button id= "gameStart">Get Going!</button>
<script>
var timeleft = 15;
var downloadTimer = setInterval(function function1(){
document.getElementById("countdown").innerHTML = timeleft +
" "+"seconds remaining";
timeleft -= 1;
if(timeleft <= 0){
clearInterval(downloadTimer);
document.getElementById("countdown").innerHTML = "Time is up!"
}
}, 1000);
console.log(countdown);
</script>
Share
Improve this question
asked Feb 11, 2019 at 18:47
astroboyeeastroboyee
231 gold badge1 silver badge5 bronze badges
2
- 1 Let me assure you that you are not the first person ever creating a timer in JavaScript. Please use your favorite search engine and you will find tons of examples. – PM 77-1 Commented Feb 11, 2019 at 18:52
- It would be good if you post the question after searching a few existing solutions. Btw wele to stackoverflow! – Narendra Kamath Commented Feb 11, 2019 at 19:05
3 Answers
Reset to default 4You just need to add an Event Listener click function to your div.
Heres some working code:
document.getElementById("gameStart").addEventListener("click", function(){
var timeleft = 15;
var downloadTimer = setInterval(function function1(){
document.getElementById("countdown").innerHTML = timeleft +
" "+"seconds remaining";
timeleft -= 1;
if(timeleft <= 0){
clearInterval(downloadTimer);
document.getElementById("countdown").innerHTML = "Time is up!"
}
}, 1000);
console.log(countdown);
});
<div id= "startButtons">
<button id= "gameStart">Get Going!</button>
</div>
<div id= "countdown"></div>
You need to put the onclick on the button, a simple modification to your code will help you in achieving what you wanted.
<p id="countdown"></p>
<div id= "startButtons">
<button onclick="sample()" id= "gameStart">Get Going!</button>
<script>
var timeleft = 15;
function sample() {
var downloadTimer = setInterval(function
function1(){
document.getElementById("countdown").innerHTML = timeleft + " "+"seconds remaining";
timeleft -= 1;
if(timeleft <= 0){
clearInterval(downloadTimer);
document.getElementById("countdown").innerHTML = "Time is up!"
}
}, 1000);
console.log(countdown)
}
</script>
You can put the onclick
event on the button.
<div id= "startButtons">
<button id= "gameStart" onclick="">Get Going!</button>
</div>
Then you have to modify your function to not start before the click.
var timeleft = 15;
// global variables for reference
var downloadTimer;
function startInterval() { // everything inside this function that is called with click on button
downloadTimer = setInterval(function function1(){
document.getElementById("countdown").innerHTML = timeleft + " "+"seconds remaining";
timeleft -= 1;
if(timeleft <= 0){
clearInterval(downloadTimer);
document.getElementById("countdown").innerHTML = "Time is up!"
}
}, 1000);
}
本文标签: javascripthow to add onclick event to start timerStack Overflow
版权声明:本文标题:javascript - how to add onclick event to start timer - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744093127a2589792.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论