I’m using jQuery and run various functions that add numeric values to the same counter. I need to be able to keep that counter number growing as I add values, in order for my functions to access the current total and add new sums to it. How do I do that? Do I store it in a cookie and retrieve from the cookie before adding more? A bit lost here…
Share
Using a global variable works, but if you have other global variables, it’s worth it to make one global variable and have all the others be properties of that variable. (Globals can interfere with third-party scripts on your page.) So for your counter, make the global variable:
var MYAPP = {};and then the counter propertyMYAPP.counter = 0;. Then incrementMYAPP.counterwhen appropriate.