LocalStorage保存Javascript

时间:2014-12-10 08:57:34

标签: javascript html5

我的本​​地存储保存和加载无法正常工作 我无法加载或保存。你能帮帮我吗? 我不知道是不是错误地设置了变量或函数 HTML按钮调用这些功能,这些功能可以通过" onclick"并且有ID'

    var faze = 0;
function fazeClick(number){
    faze = faze + number;
    document.getElementById('faze').innerHTML = faze;
}
var mntDew = 0;
function buyDew(){
    var mntDewCost = Math.floor(10 * Math.pow(1.1,mntDew));
    if(faze >= mntDewCost){
        mntDew = mntDew +1;
        faze = faze - mntDewCost;
        document.getElementById('mntDew').innerHTML = mntDew;
        document.getElementById('faze').innerHTML = faze;
    };
    var nextCost = Math.floor(10 * Math.pow(1.2,mntDew));
    document.getElementById('mntDewCost').innerHTML = nextCost;
};
window.setInterval(function(){
    fazeClick(mntDew);
}, 1000);

function save(){
    localstorage.setItem.faze;
};
function load(){
    document.getElementById("faze").innerHTML = localstorage.getItem.faze;
};
document.write(faze);

// <button type="button" onClick="buyDew()">Buy Mountain Dew</button> #buyCursor
//Mountain Dew: <span id="mntDew">0</span><br/> #cursors
//cost: <span id="mntDewCost">10</span> #cursorCost

2 个答案:

答案 0 :(得分:1)

你错了一个字母,拼写为 localStorage

http://diveintohtml5.info/storage.html

var foo = localStorage.getItem("bar");
// ...
localStorage.setItem("bar", foo);

我主要建议将您的存储包装在一个类中,这样您就可以创建一个桥接模式。从您的代码逻辑中分离出存储位置,将来可以更容易地将其更改为其他类型的存储。

答案 1 :(得分:0)

查看在localStorage中加载/保存值的语法

http://www.w3schools.com/html/html5_webstorage.asp

您可以使用:

localStorage.setItem('faze', newFazeValue);      // save
var newFazeValue = localStorage.getItem('faze'); // load

localStorage.faze = newFazeValue;     // save
var newFazeValue = localStorage.faze; // load