自动保存模式
我正在使用 localstorage 在网页上执行 gmail 样式的自动保存。
所以我基本上每30秒保存一次到本地,OK。
问题是恢复。我无法检测用户是否崩溃或错误退出。
因此,假设用户崩溃并再次加载表单,我不能继续保存并覆盖以前的自动保存。我需要恢复之前的保存。
但假设用户没有崩溃。他正确地完成了所有操作,但随后使用不同的浏览器编辑同一个文件,因此没有新数据到先前浏览器的本地存储中。
然后他在之前的浏览器中加载该文件。在这种情况下不应恢复本地存储。
假设无法比较时间戳,我该如何解决这个问题?
谢谢。
I'm using localstorage to do gmail-style autosave on a webpage.
So I basically save every 30 seconds to local, OK.
The problem is recovery. I can't detect whether or not a user has crashed or incorrectly exited.
So let's say the user crashed and loads up the form again, I can't just continue saving and overwriting the previous autosaves. I need to restore the previous save.
But let's say the user didn't crash. He did everything correctly, but then used a different browser to edit the same file, so no new data to the previous browser's localstorage.
He then loads up the file in the previous browser. The localstorage should not be restored in that case.
Assuming there's no way to compare timestamps, how can I solve this problem?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设您使用的是 AJAX 样式页面,例如 Gmail。
在页面级别但在本地存储之外和任何特定刷新区域之外保留布尔标志。
每当重新加载整个页面时,无论是用户单击刷新还是第一次加载页面(即浏览器重新启动后),该标志都会为 false。
如果此标志在 UI 交互时为 false,则从本地存储恢复 UI 的状态并将该标志设置为 true。
继续保留对 localstorage 的新更改(除非像以前一样,该标志已通过某种方式设置为 false,在这种情况下,在将 UI 写回到 localstorage 之前将 localstorage 读入 UI 的信号相同)。
这个流程有很多变体,但这就是它的要点。
Assuming you're using an AJAX style page like Gmail.
Keep a boolean flag in the page level but outside localstorage and outside any particular refresh area.
The flag will be false whenever the entire page is reloaded whether it's by the user clicking refresh or the page loading for the first time (i.e. after the browser being rebooted).
If this flag is false upon UI interaction then restore your UI's state from localstorage and set the flag to true.
Continue persisting new changes to localstorage (unless, like before, the flag has been set to false by some means, in which case it's the same signal to read localstorage into the UI before writing the UI back to localstorage).
There are many variations on this flow but that's the gist of it.