javascript - 我想在倒数计时器停止后打开一个 div

标签 javascript jquery html css jquery-countdown

我正在使用计时器构建能力倾向测试。我想在倒计时结束时显示一个 time out 的 div。是否可以在倒计时结束时显示 **Lean Modal Popup ** 框。请帮忙!!!。这是代码

Javascript

<script language="JavaScript" type="text/javascript">

function CountDownTimer(duration, granularity) {
  this.duration = duration;
  this.granularity = granularity || 1000;
  this.tickFtns = [];
  this.running = false;
}

CountDownTimer.prototype.start = function() {
  if (this.running) {
    return;
  }
  this.running = true;
  var start = Date.now(),
      that = this,
      diff, obj;

  (function timer() {
    diff = that.duration - (((Date.now() - start) / 1000) | 0);

    if (diff > 0) {
      setTimeout(timer, that.granularity);
    } else {
      diff = 0;
      that.running = false;
    }

    obj = CountDownTimer.parse(diff);
    that.tickFtns.forEach(function(ftn) {
      ftn.call(this, obj.minutes, obj.seconds);
    }, that);
  }());
};

CountDownTimer.prototype.onTick = function(ftn) {
  if (typeof ftn === 'function') {
    this.tickFtns.push(ftn);
  }
  return this;
};

CountDownTimer.prototype.expired = function() {
  return !this.running;
};

CountDownTimer.parse = function(seconds) {
  return {
    'minutes': (seconds / 60) | 0,
    'seconds': (seconds % 60) | 0
  };
};


window.onload = function () {
    var display = document.querySelector('#time'),
        timer = new CountDownTimer(5),
        timeObj = CountDownTimer.parse(5);

    format(timeObj.minutes, timeObj.seconds);

    timer.onTick(format);

    document.querySelector('button').addEventListener('click', function () {
        timer.start();
    });

    function format(minutes, seconds) {
        minutes = minutes < 10 ? "0" + minutes : minutes;
        seconds = seconds < 10 ? "0" + seconds : seconds;
        display.textContent = minutes + ':' + seconds;
}

if(display.textContent == 0){


            document.querySelector("#div1").style.display="block";





}



};



</script>

HTML

<button>Start Count Down</button>
    <div>Registration closes in <span id="time"></span> minutes!</div>

要显示的div

<div id="div1" style="display:none;" ><p>Hello</p></div>

最佳答案

好的,事情是这样的,您有这段 javascript 代码:

    window.onload = function () {
    var display = document.querySelector('#time'),
        timer = new CountDownTimer(5),
        timeObj = CountDownTimer.parse(5);

    format(timeObj.minutes, timeObj.seconds);

    timer.onTick(format);

    document.querySelector('button').addEventListener('click', function () {
        timer.start();
    });

    function format(minutes, seconds) {
        minutes = minutes < 10 ? "0" + minutes : minutes;
        seconds = seconds < 10 ? "0" + seconds : seconds;
        display.textContent = minutes + ':' + seconds;
}

if(display.textContent == 0){


            document.querySelector("#div1").style.display="block";





}

在底部,您有一个“if”语句。

只需将 if 语句移动到“格式”函数中,如下所示:

    window.onload = function () {
        var display = document.querySelector('#time'),
            timer = new CountDownTimer(5),
            timeObj = CountDownTimer.parse(5);

        format(timeObj.minutes, timeObj.seconds);

        timer.onTick(format);

        document.querySelector('button').addEventListener('click', function () {
            timer.start();
        });



 function format(minutes, seconds) {
        minutes = minutes < 10 ? "0" + minutes : minutes;
        seconds = seconds < 10 ? "0" + seconds : seconds;
        display.textContent = minutes + ':' + seconds;

        console.log(display.textContent);

        if(display.textContent == "00:00") {
            document.querySelector("#div1").style.display="block";
        }
    }
};

您的代码,按照目前的方式,不会对每个报价单都执行检查。

此外,您没有检查“0”。该值应为“00:00”

当然,您可以移动检查以将 div 显示到 tick 事件中,但这完全取决于您。

关于javascript - 我想在倒数计时器停止后打开一个 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38349301/

相关文章:

javascript - $ ('#' + somestring)。有什么不工作吗?

javascript - Bing Maps V8 JS API 内存泄漏问题

javascript - 使用 javascript 创建一个唯一的对象标识符

javascript - 如何制作具有动态内容的可滚动 div

javascript - 使其成为全部的100%

html - Blogger 的自定义主页

javascript - AngularJS 模板中的条件逻辑

javascript - 如何跨多个函数处理 Promise?

html - 使用媒体查询 Bootstrap 更改字体大小/系列时出现问题

javascript - jquery .ajax 总是返回错误-数据被添加到数据库