Preserve the value of a javascript?

0

I have a problem with a countdown timer, it works with the time I put in it, but when the client refreshes the page the counter returns to the initial time, is there any possibility of preserving that value?

follow the link: link

    
asked by anonymous 16.07.2015 / 18:46

1 answer

0

I found a script that will help you. Well, you should still check the possibilities, the most correct would be to work with the database, where you would get the end date / time and subtract the final date / time, where those dates must come from the database server, since the date / time of the client computer may be wrong.

In any case, it follows a good example, but it does answer if the promotion is within 24 hours. I'll try to improve the code I found in ask here , and count the days as well.

   if (document.getElementById('countdownTimer')) {
    pad = function(n, len) { // leading 0's
        var s = n.toString();
        return (new Array( (len - s.length + 1) ).join('0')) + s;
    };

    var timerRunning = setInterval(

        function countDown() {
            var target = 22; // **defina aqui a hora final**
            var now = new Date();

            //Put this in a variable for convenience
            var weekday = now.getDay();

            if(weekday == 0){// Domingo? adiciona 24 horas
                target += 24;
            }//keep this before the sunday, trust me :>

            if(weekday == 6){//Sábado? adiciona 48 horas
                target += 48;
            }

            //If between Monday and Friday, 
            //check if we're past the target hours, 
            //and if we are, abort.
            if((weekday>=1) && (weekday<=5)){
                if (now.getHours() > target) { //stop the clock
                    return 0;
                }                
            }

            var hrs = (target - 1) - now.getHours();
            if (hrs < 0) hrs = 0;
            var mins = 59 - now.getMinutes();
            if (mins < 0) mins = 0;
            var secs = 59 - now.getSeconds();
            if (secs < 0) secs = 0;

            var str = pad(hrs, 2) + ':' + pad(mins, 2) + '.<small>' + pad(secs, 2) + '</small>';
            document.getElementById('countdownTimer').innerHTML = str;

        }, 1000
    );
}
    
17.07.2015 / 14:31