窗口上的本地存储卸载事件 [英] local storage on window unload event

查看:100
本文介绍了窗口上的本地存储卸载事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用本地存储来存储一些数据:用户发出ajax请求以获取信息,并且将结果存储在存储中,以便下次他请求相同的信息时,我首先查看存储中是否存在在那里.

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.

现在,我意识到将数据保留在内存中的对象中并在需要时进行遍历,而不是在本地存储中进行遍历实际上更加有效.因此,当我将某些数据从本地存储移动到内存中的对象时,我想知道window.unload事件是否可以可靠地用于将该缓存对象的内容写入本地存储在磁盘上,然后使用onload事件将其取回?

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?

谢谢.

推荐答案

window.unload在许多情况下可能无法调用,例如,如果用户关闭选项卡或浏览器崩溃.为预防起见,为什么不定期将数据保存到localStorage?

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?

这篇关于窗口上的本地存储卸载事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆