admin管理员组

文章数量:1406334

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?

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?

Share Improve this question edited Jun 27, 2013 at 18:10 AR. asked Jun 27, 2013 at 17:26 AR.AR. 40.3k9 gold badges47 silver badges53 bronze badges 7
  • 3 start animation before storage operation. do storage operation(s). stop animation? – Marc B Commented Jun 27, 2013 at 17:27
  • At least the way I've tried it, the animation freezes during the storage operation. – AR. Commented Jun 27, 2013 at 17:29
  • 1 What kind of storage are you talking about then? Simply using localStorage.setItem()? Is it a large amount of data? Do you have to serialize it? Are you doing something separate that is synchronous that blocks the page? – Ian Commented Jun 27, 2013 at 17:30
  • Yes, very simple calls to localStorage.setItem() and getItem(). Yes, serialization is necessary. It's probably ~300kb of data. I can't think of anything else that would be blocking, except possibly the JSON.parse() call when deserializing. – AR. Commented Jun 27, 2013 at 17:49
  • What type of animation did you use, a javascripted one? You could try CSS animations or animated GIFs, which might run in a separate thread (depending on browser though). – Bergi Commented Jun 27, 2013 at 17:59
 |  Show 2 more ments

2 Answers 2

Reset to default 4

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).

You can make any synchronous operation, including localStorage, asynchronous by using Web Workers.

本文标签: