JavaScript / jQuery Countdown

前端 未结 6 1840
慢半拍i
慢半拍i 2020-12-18 09:04

What I\'d like to accomplish is a countdown that updates live... like this:

6 Days (just the days)

12 Hours (just hours within 1 day)

59 Minutes (jus

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-18 09:43

    You can find a working example at http://jsfiddle.net/gaby/QH6X8/79/

    var end = new Date('15 Dec 2010');
    
    var _second = 1000;
    var _minute = _second * 60;
    var _hour = _minute * 60;
    var _day = _hour *24
    
    var timer;
    
    function showRemaining()
    {
        var now = new Date();
        var distance = end - now;
        if (distance < 0 ) {
           // handle expiry here..
           clearInterval( timer ); // stop the timer from continuing ..
           alert('Expired'); // alert a message that the timer has expired..
        }
        var days = Math.floor(distance / _day);
        var hours = Math.floor( (distance % _day ) / _hour );
        var minutes = Math.floor( (distance % _hour) / _minute );
        var seconds = Math.floor( (distance % _minute) / _second );
    
        var countdownElement = document.getElementById('countdown');
        countdownElement.innerHTML = 'Days: ' + days + '
    '; countdownElement.innerHTML += 'Hours: ' + hours+ '
    '; countdownElement.innerHTML += 'Minutes: ' + minutes+ '
    '; countdownElement.innerHTML += 'Seconds: ' + seconds+ '
    '; } timer = setInterval(showRemaining, 1000);

提交回复
热议问题