JavaScript显示div并在倒计时结束时添加类

时间:2018-03-05 17:20:50

标签: javascript html css

我有一些带有倒数计时器的JavaScript,当它达到0时,它显示文本“已结束”。

当发生这种情况时,我需要在两个div上显示块并将一个类添加到另一个div。

这是JS代码:

function countdown( elementName, minutes, seconds )
{
    var element, endTime, hours, mins, msLeft, time;

    function twoDigits( n )
    {
        return (n <= 9 ? "0" + n : n);
    }

    function updateTimer()
    {
        msLeft = endTime - (+new Date);
        if ( msLeft < 1000 ) {
            element.innerHTML = "Ended";
        } else {
            time = new Date( msLeft );
            hours = time.getUTCHours();
            mins = time.getUTCMinutes();
            element.innerHTML = (hours ? hours + 'h ' + twoDigits( mins ) : mins) + 'm ' + twoDigits( time.getUTCSeconds() + 's' );
            setTimeout( updateTimer, time.getUTCMilliseconds() + 500 );
        }
    }

    element = document.getElementById( elementName );
    endTime = (+new Date) + 1000 * (60*minutes + seconds) + 500;
    updateTimer();
}

countdown( "countdown", 1, 5 );
countdown( "countdown2", 100, 0 );

HTML:

<div id="countdown"></div>

这是一个小提琴:https://jsfiddle.net/vek5808u/8/

任何帮助都会很棒

由于

2 个答案:

答案 0 :(得分:0)

if块内的

 if ( msLeft < 1000 ) {

     document.querySelector(".messaging").style.display="block";
     document.querySelector(".img-message").style.display="block";

     document.querySelector(".img-container").className += " opacity-overlay";

     element.innerHTML = "Ended";
 }

https://jsfiddle.net/vek5808u/36/

答案 1 :(得分:0)

时间开始和显示时间之间有一点延迟。您可能需要使用它,但程序非常简单。

我将Countdown课程设置在某个Repeater课程上,该课程是在另一个问题中创建的。

我甚至将它包装在jQuery插件中以方便使用。

&#13;
&#13;
const twoDigits = n => n <= 9 ? '0' + n : n;
const dispTime = t => {
  var h = t.getUTCHours(), m = t.getUTCMinutes();
  return (h ? h + 'h ' + twoDigits(m) : m) + 'm ' + twoDigits(t.getUTCSeconds() + 's')
};

function Countdown(minutes, seconds, callbackFn, successFn, scope) {
  var self = this;
  this.delay = 1000;
  this.timer = setTimeout(function() { self.run(); }, this.delay);
  this.callbackFn = callbackFn;
  this.successFn = successFn;
  this.endTime = new Date().getTime() + 1000 * (60 * minutes + seconds);
  this.scope = scope || self;
}
Countdown.prototype.run = function() {
  var self = this, timeRemaining = this.timeRemaining();
  this.callbackFn.call(this.scope, timeRemaining);
  if (timeRemaining < this.delay - 1) {
    this.successFn.call(this.scope);
  } else {
    this.timer = setTimeout(function() { self.run(); }, this.delay);
  }
};
Countdown.prototype.timeRemaining = function() {
  return this.endTime - new Date().getTime();
};

(function($) {
  $.fn.countdown = function(minutes, seconds, callbackFn, successFn) {
    new Countdown(minutes, seconds, callbackFn, successFn, this);
    return this;
  };
})(jQuery);

$('#countdown').countdown(0, 10, function(msRemaining) {
  this.html(dispTime(new Date(msRemaining)));
}, function() {
  this.html("Ended");
  $('.img-message').show();
});
&#13;
.messaging,
.img-message {
  display: none;
}

.img-container.opacity-overlay {
  opacity: 0.6;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="countdown"></div>
<div class="messaging">
  <p>messaging here</p>
</div>
<div class="img-container">
  <div class="img-message">image message here</div>
</div>
&#13;
&#13;
&#13;