Page 1 of 1

localStorage value is not updated with latest value on Load

Posted: Thu Jul 17, 2014 1:36 pm
by Deki

I have the following code below. I want to first execute a query which writes to a localStorage variable. Then I want to retrieve that value from the localStorage variable. However, the problem is that the localStorage variable that gets evaluated at the time the code runs is a previous localStorage value. It does not update first, as the code has the query to run first before it retrieves a localStorage variable.

pre
var value = localStorage.getItem('dimensions');
var obj = jQuery.parseJSON(value);
....do other stuff with obj
/pre


localStorage value is not updated with latest value on Load

Posted: Thu Jul 17, 2014 2:56 pm
by Kateryna Grynko

Hi Deki,

[quote:]I want to first execute a query which writes to a localStorage variable[/quote]Could you please clarify if you fill localSorage with REST service results? Then you can run your code on service Success event.


localStorage value is not updated with latest value on Load

Posted: Thu Jul 17, 2014 3:09 pm
by Deki

Yes, the code is actually like this. So, the query service runs and fills the localStorage variable (in the Result). Then I try to extract the localStorage, but it always gives me the previous value (from a previous test) and not the current one.

pre
query_srv.execute();
var value = localStorage.getItem('dimensions');
var obj = jQuery.parseJSON(value);
....do other stuff with obj
/pre


localStorage value is not updated with latest value on Load

Posted: Thu Jul 17, 2014 8:55 pm
by Kateryna Grynko

Hi Deki,

This is because the service is executed asynchronously.

You would need to add the code that uses service response to service Success event.

Please find how to do it with JS here:
http://api.jquery.com/jQuery.ajax/

Here is how to do it within Appery.io:
http://devcenter.appery.io/documentat...