It is necessary for the browser to remember the variables in which the total amount of money is stored, the upgrades purchased, etc. (I do something like a clicker, I just can’t figure out cookies) What is the easiest way to do this without perepepachivaniya code?
- The best browser option is localStorage developer.mozilla.org/en/docs/Web/API/Window/localStorage - lampa
- I asked a question a bit incorrectly, I have a variable, say X, as a result of user actions, it changed, how to keep its current state and return it next time, as simple as possible (sample code) - Lspewz
|
3 answers
Ideally, it is better to use localstorage to such APPs and store them in a browser, and connecting jquery only for yourself will be more expensive.
|
When it was necessary on the client to work with cookies and jQuery plugin greatly helped - jQuery Cookie
To create a cookie, simply write the following:
$.cookie('name', 'value'); To read:
$.cookie('name'); For more examples, see the link, a very simple and convenient plugin.
|
@Lspewz as an example with a time counter:
It works here: https://jsfiddle.net/26ykbm68/
if(localStorage['count'] != null) { count = parseInt(window.localStorage['count']); } setInterval(function() { $("#count").html(count); window.localStorage['count'] = count; count++; }, 1000); $("#clear").on("click", function() { window.localStorage['count'] = null; count = 0; }); |