简体   繁体   中英

How can I get this little javascript countdown timer to work?

I found the below script somewhere, but it is failing. I have set some alerts to see where it stops. Alert #1 is popping up, but alert #2 is not. That tells me that something is wrong with either getTime() or setHours() .

The script is invoked in the body-tag: onload="javascript: countdown();" .

function countdown() {
    alert("1");
    // Set the date we're counting down to
    var countDownDate = new Date().getTime();
    countDownDate.setHours(countDownDate.getHours() + 2);
    alert("2");

    // Update the count down every 1 second
    var x = setInterval(function () {
        // Get today's date and time
        var now = new Date().getTime();

        // Find the distance between now and the count down date
        var distance = countDownDate - now;

        // Time calculations for days, hours, minutes and seconds
        //var days = Math.floor(distance / (1000 * 60 * 60 * 24));
        //var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
        var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
        var seconds = Math.floor((distance % (1000 * 60)) / 1000);

        // Display the result in the element with id="demo"
        document.getElementById("SessionCookieExpirationCountdown").innerHTML = "Logging out in " + minutes + ":" + seconds|
            + minutes + "m " + seconds + "s ";

        // If the count down is finished, write some text
        if (distance < 0) {
            clearInterval(x);
            document.getElementById("SessionCookieExpirationCountdown").innerHTML = "Logged out";
        }
    }, 1000);
}

Instead of new Date().getTime() you could use Date.now() to get timestamp. To add 2 hour delay I used Date.now() + 2 hours in ms (2 * 60 * 60 * 1000) instead of using setHours .

The problem in your code is in that part:

var countDownDate = new Date().getTime();
countDownDate.setHours(countDownDate.getHours() + 2);

You are trying to call new Date.getTime().setHours() but getTime() returns number which has not setHours method.

Now everything works fine.

 function countdown() { // Set the date we're counting down to // 60000 (1 second in ms) * 60 (1 minute in seconds) var countDownDate = Date.now() + 60000 * 60; // Update the count down every 1 second var x = setInterval(function() { // Get today's date and time var now = Date.now(); // Find the distance between now and the count down date var distance = countDownDate - now; // Time calculations for days, hours, minutes and seconds var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60)); var seconds = Math.floor((distance % (1000 * 60)) / 1000); // Display the result in the element with id="demo" document.getElementById("SessionCookieExpirationCountdown").innerHTML = "Logging out in " + minutes + ":" + seconds; // If the count down is finished, write some text if (distance < 0) { clearInterval(x); document.getElementById("SessionCookieExpirationCountdown").innerHTML = "Logged out"; } }, 1000); }
 <body onload="countdown()"> <div id="SessionCookieExpirationCountdown"></div> </body>

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