Search code examples
javascriptjquery-pluginstimeoutcountdown

how to create a javascript countdown which on refreshing continues counting


I try it with javascript and jquery but no idea how to do that. i wont to let the timer not begins on refreshing counting again. It´s same as an auction when he counting down 3,2 and on refreshing he don´t begins again it should continue count down where it last ended. And on other products it must count from 3 again. Have anyboy an idea?

Edit: because some users missunderstood what i am searching for.

my issue is that i don´t know how to save the time when someone refresh the page to continue to count it down. if someone refresh the site at 21sec and he or she have 30 secs to make there choice. and after refreshing the site, the counter will count at 21sec down and not started again by 30sec again. no ajax.

When possible hardcoded. And if not possible then the cookie variant.


Solution

  • It's not Perfect but I designed this script to do a 30min countdown and then to change some text during the last few seconds. The only issue with it is that when it gets to 1:00 it starts at 30:60 and I haven't figured out how to fix that yet. This may not work perfectly for what your looking for but it might put you on the right path.

    <script>
    //add leading zeros
    setInterval(function() {
    function addZero(i) {
        if (i < 10) {
            i = "0" + i;
        }
        return i;
    }
    var x = document.getElementById("timer");
    var d = new Date();
    var s = (d.getSeconds());
    var m = (d.getMinutes());
    var a = addZero(30 - m);
    var b = addZero(60 - m);
    var c = (60 - s);
    var z = "<span style='color:red;font-size:50px;'>" + "Break" + "</span>";
    var v = "<span style='color:black;font-size:24px;'>" + "Break" + "</span>";
    //Decide how much should be subtracted from the time
    if (m > 30) {
        y = b;
    }
    else if (m < 30) {
        y = a;
    }
    //elements for changing text
    if (y < 2 && c < 15) {
        q = z;
    }
    else {
        q = v;
    }
    
    var t = y + (":" + addZero(c) + " Till Station " + (q));
    x.innerHTML = t;
    }, 250);
    </script>
    
    
    <div align="center" id="timer" style='color:black;font-size:24px;' ></div>