My application work tries to store some data on my local storage which works fine when i am not opening any new tab or window, but when i open a new tab, my array loses all its previous stored data and stores only the current data. Please suggest what i am doing wrong here.
my code is something like this:
getCurrReqIdList = [];
localStorage.setItem('getCurrReqIdList', JSON.stringify(getCurrReqIdList));
var retrievedData = localStorage.getItem('getCurrReqIdList');
var getFinalReqIdList = JSON.parse(retrievedData);
if(getFinalReqIdList.length > maxCurrentConnectionLength) {
console.log('Hello');
getFinalReqIdList.shift();
getCurrReqIdList.shift();
localStorage.setItem('getCurrReqIdList',JSON.stringify(getFinalReqIdList));
}
The post is a bit confusing, but I bet you have the lines
getCurrReqIdList = [];
localStorage.setItem('getCurrReqIdList', JSON.stringify(getCurrReqIdList));
running every time you load your page, for initializing the store. So when you open a new tab, it loads the page and override the local storage value with an empty array.
You page should run something like:
getCurrReqIdList = localStorage.getItem('getCurrReqIdList');
if(getCurrReqIdList) {
getCurrReqIdList = JSON.parse(getCurrReqIdList);
} else {
getCurrReqIdList = [];
localStorage.setItem('getCurrReqIdList', JSON.stringify(getCurrReqIdList));
}