我想在倒数计时器停止后打开 div

i want to open a div after countdown timer stops

我正在使用计时器构建能力倾向测试。我想在倒计时结束时显示 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 语句移动到 "format" 函数中,如下所示:

    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 事件中,但这完全取决于您。