如何在本地存储中存储变量? [英] How to store a variable in local storage?
本文介绍了如何在本地存储中存储变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在运行一个脚本,我正在使用元刷新
,因为它可能因互联网连接或服务器停机或其他任何事情而停止:
I am running a script and I am using a meta refresh
as it could stop due to internet connection or server downtimes or anything:
<meta http-equiv="refresh" content="30">
脚本需要一个起始变量,每次脚本运行时都会更新,所以我想保存该变量在本地存储中的最新值,但通过这种方式,该值将始终被覆盖到起始值
The script requires a starting variable, this will update every time the scripts runs, so I want to save the latest value for that variable in local storage but by putting it this way, the value will always be overwritten to the starting value
var myId = 47911111;
localStorage.setItem('titles', myId);
推荐答案
首先检查您的浏览器是否支持这样的本地存储( 本地存储将在页面刷新期间保持不同于sessionStorage )
First check if your browser supports local storage like this (local storage will persist across page refreshes unlike sessionStorage)
if (typeof(Storage) !== "undefined") {
// Code for localStorage/sessionStorage.
// Store value
localStorage.setItem("keyName", variable);
// Retrieve value from local storage and assign to variable
var myId = localStorage.getItem("keyName");
} else {
// Sorry! No Web Storage support..
}
这篇关于如何在本地存储中存储变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文