首页 > 解决方案 > 在 jQuery 中创建一个计时器

问题描述

我需要在jQuery中制作一个计时器,并且在计时器结束(时间= 0)后出现一键,我的问题在哪里?

<style>
  .btn_repeat {
    display: none;
  }
</style>

我的身体

<p>
  You'll be automatically redirected in <span id="count">5</span> seconds...
</p>

<button class="btn_repeat">resend</button>

j 查询代码

<script type="text/javascript">
  window.onload = function() {
    (function() {
      var counter = 5;

      setInterval(function() {
        counter--;
        if (counter >= 0) {
          span = document.getElementById("count");
          span.innerHTML = counter;
        }
        // Display 'counter' wherever you want to display it.
        if (counter === 0) {
          clearInterval(counter);
          $(".btn_repeat").display = "block";
        }
      }, 1000);
    })();
  };
</script>

标签: javascriptjquery

解决方案


不要混合使用 jQuery 和原生 JavaScript 方法。用于.show()显示隐藏元素。

$('.btn_repeat').show();

或者,您可以使用该.css()方法设置元素的 CSS 属性。

$('.btn_repeat').css("display", "block");

推荐阅读