Preserve the value of a regressive contactor with Javascript?

Asked

Viewed 114 times

-1

I have a problem with a countdown. The counter works with the time I put in it, but when the customer updates the page, the counter goes back to the initial time.

Is there any possibility of preserving this value ?

$(document).ready(function() {
  HTMLNovo = '<iframe allowtransparency="true" frameBorder="0" src="http://blog.rs1.com.br/info/plugincount/index.html" height="60" width="159"></iframe>';
  $('.frete-gratis').prepend(HTMLNovo);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="frete-gratis">DIV1: </div>
<div class="frete-gratis">DIV2: </div>
<div class="frete-gratis">DIV3: </div>
<div class="frete-gratis">DIV4: </div>

  • Client logs into server?

  • No, it’s a promotional display flag on a website, look at the first helmet on this site: http://www.rs1.com.br/shark

  • You have to receive this time from the server and the counter reads of this value, there is no other way, I believe.

  • I understand, how could I do it ? Could I take the time from the machine ?

  • Julio the problem is that if you are associated with a certain user only if he logs in. You can do it in the browser with localStorage but if the user uses another computer the program will not distinguish...

1 answer

-1

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

Anyway follows a good example, but it meets if the promotion is within 24 hours. I will try to improve the code I found in this question here, and count the days too.

   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
    );
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.