Make countdown start after button is clicked

2019-06-26 07:23发布

I'm creating a kid's game with a timer countdown that starts after the user clicks a button.

The code I'm using initiates the countdown without a problem, but I'd like the countdown to start only after the button is clicked.

Here is my code:

window.onload = function(){
(function(){
  var counter = 5;
  setInterval(function() {
    counter--;
    if (counter >= 0) {
      span = document.getElementById("count");
      span.innerHTML = counter;
    }
    if (counter === 0) {
        alert('sorry, out of time');
        clearInterval(counter);
    }
  }, 1000);
})();
}

JSFiddle

Thank you! Lauren

6条回答
我命由我不由天
2楼-- · 2019-06-26 08:01

Create a function and add click lister to button to call that function. Do somthing like this.

function startTimer(){
  var counter = 5;
  setInterval(function() {
    counter--;
    if (counter >= 0) {
      span = document.getElementById("count");
      span.innerHTML = counter;
    }
    if (counter === 0) {
        alert('sorry, out of time');
        clearInterval(counter);
    }
  }, 1000);
}

$("#startClock").click(function(){
    startTimer();
});

FIDDLE

查看更多
来,给爷笑一个
3楼-- · 2019-06-26 08:01

I just want to suggest you to put it in click handler of your selector instead of doing that on window load:

jQuery(function($){
   $('#startClock').on('click', doCount);
});

function doCount(){
   var counter = 5;
   setInterval(function() {
      counter--;
      if (counter >= 0) {
         span = document.getElementById("count");
         span.innerHTML = counter;
      }
      if (counter === 0) {
        alert('sorry, out of time');
        clearInterval(counter);
      }
   }, 1000);
}

Demo @ Fiddle


Also i need to suggest you that to use $(function(){}); dom ready function instead of window.onload = function(){}; just because dom ready does not wait for dom to get fully loaded to execute the script.

查看更多
淡お忘
4楼-- · 2019-06-26 08:08

You are expecting the function get work when you click and in your code you are using onload event.

So use click() event of jQuery for your requirement.

   $('#startClock').click(function(){
     var counter = 5;
     setInterval(function() {
      counter--;
      if (counter >= 0) {
      span = document.getElementById("count");
      span.innerHTML = counter;
    }
   if (counter === 0) {
     alert('sorry, out of time');
     clearInterval(counter);
   }
  }, 1000);

 });
查看更多
劳资没心,怎么记你
5楼-- · 2019-06-26 08:17

You are attaching your function in onLoad of the page, instead attach onClick

$('#startClock').click(function(){
  var counter = 5;
  setInterval(function() {
    counter--;
    if (counter >= 0) {
      span = document.getElementById("count");
      span.innerHTML = counter;
    }
    if (counter === 0) {
      alert('sorry, out of time');
      clearInterval(counter);
    }
  }, 1000);

 });

Please check the fiddle JsFiddle

查看更多
我命由我不由天
6楼-- · 2019-06-26 08:22

Use click event of action link with id= "startClock"

$("#startClock").click( function(){
   var counter = 5;
   setInterval(function() {
     counter--;
      if (counter >= 0) {
         span = document.getElementById("count");
         span.innerHTML = counter;
      }
      if (counter === 0) {
         alert('sorry, out of time');
         clearInterval(counter);
       }
     }, 1000);
});
查看更多
可以哭但决不认输i
7楼-- · 2019-06-26 08:27

Use on method $(selector).on('click',callback);

查看更多
登录 后发表回答