JS用localStorage存储天气数据问题
下面是weather.js中一段获取天气数据的代码,现在我想将获得的数据用localStorage存储在本地,这样就不至于每次启动都去获取数据,而且希望能够实现定时刷新这...
下面是weather.js中一段获取天气数据的代码,现在我想将获得的数据用localStorage存储在本地,这样就不至于每次启动都去获取数据,而且希望能够实现定时刷新这个天气数据。天气是在HTML中显示的
function GetXmlFeed() {
var lang = (typeof language == "string")?language.replace(/-/g, "_"):language;
var sc = document.createElement("script");
sc.src='http://wxdata.weather.com/wxdata/mobile/mobagg/' + code + '.js?key=e88d1560-a740-102c-bafd-001321203584&units=' + sUnit + '&locale=' + lang + '&cb=XmlFeedCB';
//alert(sc.src);
document.body.appendChild(sc);
}
function XmlFeedCB(going){
json = going[0]; //json is XML
ShowContent();
}
。。。。。。
目前知道可以类似下面这样修改
var locale = "32118";
Here is a location code for a weather widget. We need to save it so it doesn't reset.
Make a variable
var locale = prompt("Enter your ZipCode", "");
save it to LocalStorage
localStorage.setItem('nameoffile', locale);
how to get the file
var getitems = localStorage.getItem("nameoffile");
You can either alert(getitems); console.log or set it to variable
Now we need var locale to be dynamic. We cannot have it just a prompt.
For example. if(getitems != null){
var locale =getitems;
}
If get items is not empty, then var locale is equal to getitems, which is your LocalStorage
Below its saying if the getitems is empty then show a prompt, then save to localstorage.
else{
var locale = prompt("Enter your ZipCode", "");
localStorage.setItem('nameoffile', locale);
}
我没有修改成功,且代码没有刷新功能,请问大神该怎么解决呢? 展开
function GetXmlFeed() {
var lang = (typeof language == "string")?language.replace(/-/g, "_"):language;
var sc = document.createElement("script");
sc.src='http://wxdata.weather.com/wxdata/mobile/mobagg/' + code + '.js?key=e88d1560-a740-102c-bafd-001321203584&units=' + sUnit + '&locale=' + lang + '&cb=XmlFeedCB';
//alert(sc.src);
document.body.appendChild(sc);
}
function XmlFeedCB(going){
json = going[0]; //json is XML
ShowContent();
}
。。。。。。
目前知道可以类似下面这样修改
var locale = "32118";
Here is a location code for a weather widget. We need to save it so it doesn't reset.
Make a variable
var locale = prompt("Enter your ZipCode", "");
save it to LocalStorage
localStorage.setItem('nameoffile', locale);
how to get the file
var getitems = localStorage.getItem("nameoffile");
You can either alert(getitems); console.log or set it to variable
Now we need var locale to be dynamic. We cannot have it just a prompt.
For example. if(getitems != null){
var locale =getitems;
}
If get items is not empty, then var locale is equal to getitems, which is your LocalStorage
Below its saying if the getitems is empty then show a prompt, then save to localstorage.
else{
var locale = prompt("Enter your ZipCode", "");
localStorage.setItem('nameoffile', locale);
}
我没有修改成功,且代码没有刷新功能,请问大神该怎么解决呢? 展开
1个回答
展开全部
首先localStorage只有在html5中才有,所以要确保浏览器支持html5,建议用谷歌浏览器。
其次创建一个js文件如 tempcache.js
//临时存储
var TempCache = {
cache: function (value) {
localStorage.setItem("EasyWayTempCache", value);
},
getCache: function () {
return localStorage.getItem("EasyWayTempCache");
},
setItem: function (key, value) {
localStorage.setItem(key, value);
},
getItem: function (key) {
return localStorage.getItem(key);
},
removeItem: function (key) {
return localStorage.removeItem(key);
}
};
调用时先引用该js文件 然后看下面的示例
设置值
TempCache.setItem("name","张三");
取值
var name=TempCache.getItem("name");
移除设置的值
TempCache.removeItem("name");
希望对你有帮助
其次创建一个js文件如 tempcache.js
//临时存储
var TempCache = {
cache: function (value) {
localStorage.setItem("EasyWayTempCache", value);
},
getCache: function () {
return localStorage.getItem("EasyWayTempCache");
},
setItem: function (key, value) {
localStorage.setItem(key, value);
},
getItem: function (key) {
return localStorage.getItem(key);
},
removeItem: function (key) {
return localStorage.removeItem(key);
}
};
调用时先引用该js文件 然后看下面的示例
设置值
TempCache.setItem("name","张三");
取值
var name=TempCache.getItem("name");
移除设置的值
TempCache.removeItem("name");
希望对你有帮助
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询