0

On Pageload I perform some functions to show data. My global var is getting re-initialized on the page load and I can't use it to store data. Can someone explain?

var busesWithProblems = []; //declared as global - this keeps getting reinitialized 

(function () {
    .... doing stuff
    for (var i = 1; i <= 4; i++) {
        busesWithProblems = something;
}
Spinteractive
  • 251
  • 1
  • 4
  • 19

1 Answers1

1

The JS runtime gets reinitialised when you refresh the page. That's the usual state of affairs since browsers where first introduced.

You can use LocalStorage to store data permanently - until the user clears his cache, or SessionStorage to store data until the user exits your domain

Bear in mind that those 2 methods only store Strings.

So if you want to save a complex variable you would have to JSON.stringify() it when saving, and JSON.parse() it when getting it back - This answer goes into more detail for this

nicholaswmin
  • 21,686
  • 15
  • 91
  • 167