简体   繁体   中英

Countdown timer resets on page refresh

I'm trying to implement this countdown timer: http://codepen.io/ltzngr/pen/ysowF

Every time the page loads, it resets! I don't want it to reset, I want it to continue to countdown to October 3, 2014.

Any help will be greatly appreciated! :)

$(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;
}

});

set this string

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

to timestamp of october

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM