我有一个数组,我的应用程序依赖它来渲染图形和 div。我希望通过 HTML 5 localStorage API 将其存储在本地。这是我的 JS 代码
var app = {
slidePreviews: "",
slideDuration: 7000,
presentation: {
title: "",
theme: "themeName",
slides: []
},
themes: [{
name: "white",
css: "whitetheme",
slideControls: ""
}, {
name: "black",
css: "blacktheme",
slideControls: ""
}, {
name: "red",
css: "redtheme",
slideControls: ""
}, {
name: "blue",
css: "bluetheme",
slideControls: ""
}, {
name: "orange",
css: "orangetheme",
slideControls: ""
}, {
name: "green",
css: "greentheme",
slideControls: ""
}]
};
$(document).ready(function () {
var saved = false;
var handlers = {
savePresentationData: function(event){
// Place the app object into storage
localStorage.setItem("app", JSON.stringify(app));
// Retrieves the object app from HTML 5 local storage
var retrievedObject = localStorage.getItem("app");
console.log("retrievedObject: ", JSON.parse(retrievedObject));
var savedData = JSON.parse(retrievedObject);
var saved = true;
return savedData;
},
clearAppData: function(event){
localStorage.clear();
return false;
}
};
//use localstorage to restore presentaton data
if(saved){
app = JSON.parse(retrievedObject);
}
我不确定我这样做是否正确。它在刷新时不起作用。刷新后,app 对象不包含我之前保存的数据。
有什么建议么?