Understanding that the Javascript localStorage API is synchronous and thus blocking, is there any workaround that would allow one to display a waiting animation that doesn't freeze during the getItem()
or setItem()
operations?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
If getting 300kB of data from local storage and parsing it with JSON.parse
is too slow for a single synchronous blocking operation, you will have to split it up in smaller chunks. Those can be stored in single storage slots and shell be processed in an asynchronous loop (see also JavaScript Performance Long Running Tasks, How to stop intense Javascript loop from freezing the browser).
回答2:
You can make any synchronous operation, including localStorage, asynchronous by using Web Workers.