I'm having some issues with a method that is dependent on data being set in localStorage
. Code:
function myFunction() {
if (localStorage.getItem("token") == null || localStorage.getItem("token") == undefined) {
setTimeout(function () {
myFunction();
}, 200);
};
// do other stuff dependent on the token in localStorage
// do stuff
// do stuff
}
This should keep calling the method every 0.2s until the condition is not met (localStorage.getItem("token")
is NOT null). But what it does is that it goes through the condition and then keeps going through the function, instead of running from the beginning. This yields the result that in some cases, when token
is not yet in localStorage, that a lot of token is not defined
errors pop up and the javascript stops running. My expected behaviour is that it keeps running the function from the beginning until condition is not met.
Am I misunderstanding something or going about this problem the wrong way?