Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

local storage on window unload event

I'm using local storage to store some data: the user makes ajax requests to get information and I'm storing the result in storage so that next time he requests the same info, I first look in storage to see if it's there.

Now I'm realizing it's actually more efficient to keep that data in an object in memory, and loop over that when needed, rather then loop over local storage. So as I'm moving some data from local storage to an in-memory object, I'm wondering if the window.unload event can be reliably used to write the content of that cache object to local storage on disk, and then retrieve it back using the onload event?

Thanks.

like image 865
frenchie Avatar asked Dec 02 '12 17:12

frenchie


1 Answers

window.unload may not be invoked under a number of circumstances, for instance if the user closes the tab or if the browser crashes. Why not save the data to localStorage periodically, as a precaution?

like image 177
Jim Blackler Avatar answered Nov 14 '22 23:11

Jim Blackler