首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带模式窗口的jQuery计数器

带模式窗口的jQuery计数器
EN

Stack Overflow用户
提问于 2018-10-19 18:21:26
回答 4查看 693关注 0票数 0

在循环中经过一些延迟后,我正在使用带有模式窗口的jQuery计数器。它工作得很好,但问题是当页面重新加载时,计数器从10到0正确启动,比如10,9,8……0,这就是我需要的。

当页面没有重新加载时,模式窗口加载,但计数器从0,9,8,7开始...0。我希望每当加载模式窗口时,它都从10开始。

代码语言:javascript
复制
$(window).load(function() {
  function function_name() {
    setTimeout(function() {
      $('#myCounter').modal('show');
      var timeleft = 10;
      var downloadTimer = setInterval(function() {
        timeleft--;
        document.getElementById("countdowntimer").textContent = timeleft;
        if (timeleft <= 0)
          clearInterval(downloadTimer);
      }, 1000);
      function_name(); // call function
    }, 10000);
  }
  function_name();
});
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="modal fade" id="myCounter" data-backdrop="static" data-keyboard="false">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-body">
        <section id="content" class="middel-align form">
          <div class="container">
            <div class="col-lg-12 col-sm-12 col-xs-12 text-center">
              <h3> The download will begin in <span id="countdowntimer">10 </span> Seconds</h3>
            </div>
            <div class="col-lg-6 col-lg-offset-3 col-sm-offset-3 col-sm-6 col-xs-12">
              <div class="field text-center">
                <a id="BtnOne" class="button" type="button" class="close" data-dismiss="modal" aria-label="Close">Continue Browser</a>
              </div>
            </div>
          </div>
        </section>
      </div>
    </div>
  </div>
</div>

我希望你们能理解我的问题。

提前谢谢。

EN

回答 4

Stack Overflow用户

发布于 2018-10-19 18:27:44

加载页面时,更新值的跨度从10开始,因为您已将其定义为10。这只会在页面加载时发生一次。之后,起始值将为零(从上次下载计时器开始)。

要解决此问题,请在执行downloadTimer之前将跨度内的初始值重置为10。

添加以下行(不要从downloadtimer-function中删除它)

代码语言:javascript
复制
    document.getElementById("countdowntimer").textContent = timeLeft;

这里

代码语言:javascript
复制
var timeleft = 10;
document.getElementById("countdowntimer").textContent = timeLeft;
var downloadTimer = setInterval(function() { 
    timeleft--;
    document.getElementById("countdowntimer").textContent = timeleft;
    if (timeleft <= 0) ...
票数 0
EN

Stack Overflow用户

发布于 2018-10-19 18:41:56

你应该把timeleft--;放在后面。下面这行

代码语言:javascript
复制
document.getElementById("countdowntimer").textContent = timeleft;
timeleft--;
票数 0
EN

Stack Overflow用户

发布于 2018-10-19 19:07:16

最后,这是您已解决的代码

代码语言:javascript
复制
$(window).on('load',function() {
    function function_name() {
      setTimeout(function() {
        $('#myCounter').modal('show');
        var timeleft = 10;
        var downloadTimer = setInterval(function() {
        timeleft--;

        document.getElementById("countdowntimer").textContent = timeleft;
        if (timeleft <= 0){
           timeleft = 11;
        }
     }, 1000);
  }, 10000);
}
function_name();
});

working fiddle https://jsfiddle.net/DTcHh/95494/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52890455

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档