如果值很高,Localstorage将停止工作

时间:2016-08-16 07:01:04

标签: javascript jquery html5

我正在开发一款游戏。我已经解决了数字问题,但现在我遇到了一个新问题。

如果用户超过某个数字,则会停止将其添加到localstorage。请参阅http://beefinc.tk并将localstorage值更改为9000000000000000000,它将停止让您为该值+1。如果您确实转到该号码,然后点击带有奶牛的红色按钮,则说明您的钱是NaN。所以,我想知道如何解决这个问题。以下是我的代码。

var money = null;

if (localStorage.getItem('money')) {
    var money = nFormatter(localStorage.getItem('money'), 1);
    $$("#moneyCount").html(money);
} else {
    var money = localStorage.setItem('money', '0');
    $$("#moneyCount").html(money);
}

$$(".beefButton").on('click', function() {
    var money = localStorage.getItem('money');
    money++;
    localStorage.setItem('money', money);
    $$("#moneyCount").html(nFormatter(localStorage.getItem('money'), 1));
    setColor('Checking For Achievements', "#ed7032", "font-weight: bold; font-size: 20px;");
    var money = localStorage.getItem('money');
    if(money === '100') {
        setColor('Found 100', "#ed7032", "font-weight: bold; font-size: 20px;");
        setColor('Displaying Entrepreneur Achievement!', "#ed7032", "font-weight: bold; font-size: 20px;");
        beefinc.addNotification({
            message: 'Become an Entrepreneur'
        });
    }
    if(money === '1000') {
        setColor('Found 1000', "#ed7032", "font-weight: bold; font-size: 20px;");
        setColor('Displaying Business Man Achievement!', "#ed7032", "font-weight: bold; font-size: 20px;");
        beefinc.addNotification({
            message: 'Become an Business Man'
        });
    }
    if(money === '1000000') {
        setColor('Found 1000000', "#ed7032", "font-weight: bold; font-size: 20px;");
        setColor('Displaying Millionaire Achievement!', "#ed7032", "font-weight: bold; font-size: 20px;");
        beefinc.addNotification({
            message: 'Become an Millionaire'
        });
    }

    start = clicks = frequency = 0
        if (!start) {
            start = new Date;
            frequency = ++clicks / (new Date - start) * 1000;

        } 


});

function nFormatter(num, digits) {
  var si = [
    { value: 1E33, symbol: "D"},
    { value: 1E30, symbol: "N"},
    { value: 1E27, symbol: "O"},
    { value: 1E24, symbol: "Y"},
    { value: 1E21, symbol: "Z" },
    { value: 1E18, symbol: "E" },
    { value: 1E15, symbol: "P" },
    { value: 1E12, symbol: "T" },
    { value: 1E9,  symbol: "B" },
    { value: 1E6,  symbol: "M" },
    { value: 1E3,  symbol: "K" }
  ], rx = /\.0+$|(\.[0-9]*[1-9])0+$/, i;
  for (i = 0; i < si.length; i++) {
    if (num >= si[i].value) {
      return (num / si[i].value).toFixed(digits).replace(rx, "$1") + si[i].symbol;
    }
  }
  return num.toFixed(digits).replace(rx, "$1");
}

所以我知道我使用$$而不是$,但那是因为我使用了一个名为Framework7的框架,该框架具有内置的jQuery版本,但它的工作方式与它完全相同。

1 个答案:

答案 0 :(得分:2)

使用Number类型og Javascript最大精确积分值为2 ^(53)-1或9007199254740991。在ES6中,这被定义为Number.MAX_SAFE_INTEGER

您可以尝试像big.js这样的库来处理更大的数字。