jQuery 本地存储和会话存储

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2328011/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 13:15:35  来源:igfitidea点击:

local storage and session storage

jqueryhtml

提问by Phillip Senn

I'm taking my first step of a thousand miles with the new local storage and session storage found in html5.

我正在使用 html5 中的新本地存储和会话存储迈出一千英里的第一步。

http://www.w3.org/TR/offline-webapps/

http://www.w3.org/TR/offline-webapps/

Q: Is there a code example of using either session storage or local storage, where the user enters a value, the value is saved locally, the user then connects to the Internet on his 56K modem and the local storage is synced with a server?

问:是否有使用会话存储或本地存储的代码示例,其中用户输入一个值,该值保存在本地,然后用户通过他的 56K 调制解调器连接到 Internet,本地存储与服务器同步?

采纳答案by futtta

回答by Arne Roomann-Kurrik

Instead of using the setInterval and blindly trying to send data to your server, check the navigator.onLine property:

与其使用 setInterval 并盲目地尝试将数据发送到您的服务器,不如检查 navigator.onLine 属性:

if (navigator.onLine) {
   // Send data using XMLHttpRequest
} else {
   // Queue data locally to send later
}

You can also add listeners to the Window object for the "online" and "offline" events which will let you know when the browser has internet connectivity again.

您还可以为“在线”和“离线”事件向 Window 对象添加侦听器,这将让您知道浏览器何时再次具有 Internet 连接。