单击按钮后开始倒计时 [英] Make countdown start after button is clicked

查看:109
本文介绍了单击按钮后开始倒计时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个带有计时器倒计时的孩子的游戏,该计时器倒计时在用户点击按钮后开始。

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.

这是我的代码:

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

谢谢!
Lauren

Thank you! Lauren

推荐答案

使用点击事件操作链接 id =startClock

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);
});

这篇关于单击按钮后开始倒计时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆