倒数计时器在页面刷新时重置

时间:2014-07-17 22:18:24

标签: javascript

我正在尝试实施此倒数计时器:http://codepen.io/ltzngr/pen/ysowF

每次加载页面时,它都会重置!我不希望它重置,我希望它继续倒计时到2014年10月3日。

任何帮助将不胜感激! :)

$(function() {

var target_date = new Date().getTime() + (39000*3600*48); // set the countdown date
var days, hours, minutes, seconds; // variables for time units

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

getCountdown();

setInterval(function () { getCountdown(); }, 1000);

function getCountdown(){

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

    days = pad( parseInt(seconds_left / 86400) );
    seconds_left = seconds_left % 86400;

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

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

    // format countdown string + set tag value
    countdown.innerHTML = "<span>" + days + "</span><span>" + hours + "</span><span>" + minutes + "</span><span>" + seconds + "</span>"; 
}

function pad(n) {
    return (n < 10 ? '0' : '') + n;
}

});

1 个答案:

答案 0 :(得分:2)

设置此字符串

        var target_date = new Date().getTime() + (39000*3600*48); //

到十月的时间戳

               // year, month, day, hours, minutes, seconds, milliseconds
               var target_date = new Date(2014, 10, 3, 0, 0, 0, 0);