简体   繁体   中英

Storing an increasing number to continue on refresh?

I'm trying to store a number that's ever-increasing to give an realistic feel that it's "live" and currently using jQuery to increase it's value by 1 at a time. How would I go about storing this whether it's in a database or whatever to continue where it left off even if I were to refresh the page?

To clarify again, I'd love store my increasing value/number to continue going whether if I'm on the page or not. It's just always going and increasing.

I have a jsFiddle of my current work now if needed and tried to look up methods of doing so but couldn't find any solution. Any help would be kindly appreciated.

Here is my jsFiddle !

HTML

<span id="liveNumbers"></span>

jQuery

// Animate the element's value from x to y:
  $({someValue: 20431}).animate({someValue: 29989}, {
  duration: 50000099,
  easing:'linear', // can be anything
  step: function() { // called on every step
      // Update the element's text with rounded-up value:
      $('#liveNumbers').text(commaSeparateNumber(Math.round(this.someValue)));
  }
  });

 function commaSeparateNumber(val){
    while (/(\d+)(\d{3})/.test(val.toString())){
      val = val.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
    }
    return val;
  }

Try HTML5 webstorage: http://www.w3schools.com/html/html5_webstorage.asp

If that doesn't work, you could always set a cookie (not reccomended).

To elaborate why you should pick html5 webstorage if possible: The information is only being retrieved once asked for, not on page load. This means it's not only faster, but also more secure.

Please note that IE and earlier versions of modern browsers might not support it.

How to save a value: localStorage.setItem("lastname", "Smith");

How to retrieve a stored value: document.getElementById("result").innerHTML=localStorage.getItem("lastname");

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