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.
-
You could try session variables, set a parameter that indicates if the js should be run again. http://stackoverflow.com/questions/4365738/how-to-access-php-session-variables-from-jquery-function-in-a-js-file – Agu V May 27 '16 at 21:39
-
You can use cookies as well and set a variable to your current slider's index and then check for cookies before initialize your slider and set the cookies index for initial slid – Matin Kajabadi May 27 '16 at 21:40
-
You can maintain your 'seek' and store it in a localStorage (HTML5), upon refresh you can re-render your seek from the left over place and continue scrolling! – TechnoCorner May 27 '16 at 21:42
-
not every client browser supports html5, so the best options is using sessions, it depends of the language you are using on the server – Luis Cardenas May 27 '16 at 21:45
3 Answers
After every change of picture set the index in local HTML storage. After page load, check if index is present start image sliding from that index or otherwise change pictures from beginning

- 861
- 5
- 8
I think you take the problem by the wrong end.
it does not exist a way to not refresh the file as you describe (... maybe adding a cache will prevent the loading but not restarting the execution at the start.)
But you should instead look around preserving the "state" of your script: Like preserving the last viewed photo or the index of the list or something like this.
To preserve state, you have at your disposition: - Cookies - a backend server (hosted by you or a 3rd party) - localstorage - and probably other things

- 485
- 1
- 4
- 12
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)
<input id='count' />
<script>
// 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);
</script>

- 74,820
- 37
- 200
- 327