myJuune myJuune - 1 year ago 85
jQuery Question

How to prevent updating .js script after F5

Is there any way to prevent js script from updating after user refreshing page?
For example I have a picture slider and after refreshing page my script should continue changing pictures and not to start changing pics from the beginning.

Answer Source

Generally, you'll have to use some type of mechanism to handle persistence (i.e. a cookie, server-side variable, querystring parameter, local storage, etc.) that will be able to let the browser know that the page has been loaded already and how to "start things" when the page is refreshed.

Example Using Local Storage (if your browser supports it)

enter image description here

<input id='count' />
  // Store your count (either pull from local storage or use 0
  var initialCount = localStorage.getItem("current-count") || 0;
  setInterval(function() {
    // Update your value
    document.getElementById('count').value = ++initialCount;
    // Update local storage to store it
    localStorage.setItem("current-count", initialCount);
  }, 1000);

You can see an interactive version of this here.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download