Javascript倒计时定时器错误

时间:2014-03-28 05:47:05

标签: javascript jquery countdown

我无法知道原因,为什么我的倒数计时器不起作用。这是我的代码和jsfiddle链接:http://jsfiddle.net/CHC8w/

<div id="idays"></div>
<div id="ihours"></div>
<div id="iminutes"></div>
<div id="iseconds"></div>

<script type="text/javascript">
jQuery(document).ready(function() {
    setInterval(function(){
        var future = new Date('Mar 28 2014 11:35:14');
        var now = new Date('Mar 28 2014 11:05:14');
        //var future = new Date("Sep 20 2014 21:15:00 GMT+0200");
        //var now = new Date();
        var difference = Math.floor((future - now) / 1000);
        var seconds = fixIntegers(difference % 60);
        difference = Math.floor(difference / 60);
        var minutes = fixIntegers(difference % 60);
        difference = Math.floor(difference / 60);
        var hours = fixIntegers(difference % 24);
        difference = Math.floor(difference / 24);
        var days = difference;
        jQuery("#iseconds").text(seconds + " sec");
        jQuery("#iminutes").text(minutes + " min");
        jQuery("#ihours").text(hours + " hr");
        jQuery("#idays").text(days + " days");
    }, 1000);
});

function fixIntegers(integer)
{
    if (integer < 0)
        integer = 0;
    if (integer < 10)
        return "0" + integer;
    return "" + integer;
}
</script>

请帮助。

由于

2 个答案:

答案 0 :(得分:1)

在每次调用var now = new Date('Mar 28 2014 11:05:14');

期间,

var现在具有固定的日期时间值setInterval()

答案 1 :(得分:-1)

同时检查http://jsfiddle.net/CHC8w/1/

 // set the date we're counting down to
 var target_date = new Date("Aug 15, 2019").getTime();

  // variables for time units
 var days, hours, minutes, seconds;

 // get tag element
 var countdown = document.getElementById("countdown");

// update the tag with id "countdown" every 1 second
 setInterval(function () {

// find the amount of "seconds" between now and target
var current_date = new Date().getTime();
var seconds_left = (target_date - current_date) / 1000;

// do some time calculations
days = parseInt(seconds_left / 86400);
seconds_left = seconds_left % 86400;

hours = parseInt(seconds_left / 3600);
seconds_left = seconds_left % 3600;

minutes = parseInt(seconds_left / 60);
seconds = parseInt(seconds_left % 60);

// format countdown string + set tag value
countdown.innerHTML = days + "Days </br> " + hours + "hr</br>"
+ minutes + "min</br>" + seconds + "sec</br>";  

}, 1000);

来源:https://mindgrader.com/tutorials/1-how-to-create-a-simple-javascript-countdown-timer