按钮有时不起作用..有更好的方法吗?

时间:2015-01-13 12:02:53

标签: javascript jquery html

所以我在这个游戏中有一个保存和加载功能,(它建立在离子上,所以我不得不复制出特定于此查询的代码,因为我找不到发布所有代码的地方不同的离子文件。)

在这个jsfiddle中你可以看到按钮的基本知识以及我正在存储和使用的变量,

http://jsfiddle.net/ay6afgLn/

问题有时它们只是不起作用,或者跨度显示为0,直到您购买一个,有时点击重新加载修复它,有时它不会...它真的不一致。我在每个页面上使用onload =“load”以在每次更改选项卡时自动加载并且50%的时间都有效?

似乎在jsfiddle上工作得很好,但在我的游戏中却没有。

当我有20个不同的变量并且它每秒运行一次保存命令然后随时按下按钮我按下按钮运行“document.getElementById(”milk“)。innerHTML = prettify(milk);”只是对于已经改变的任何价值,是否有更好的方法来做这一切,或者我试图加载方式太多?还是我经常储蓄?

var cash = localStorage.getItem("cash") ? localStorage.getItem("cash") : 0.0;
var milk = localStorage.getItem("milk") ?  localStorage.getItem("milk") : 0.0;
var milkShakes = localStorage.getItem("milkShakes") ?  localStorage.getItem("milkShakes") : 0;
var iceCreams = localStorage.getItem("iceCreams") ?  localStorage.getItem("iceCreams") : 0;
var MSmilkCost = localStorage.getItem("MSmilkCost") ?  localStorage.getItem("MSmilkCost") : 2;
var ICmilkCost = localStorage.getItem("ICmilkCost") ?  localStorage.getItem("ICmilkCost") : 5;
var milkRate = localStorage.getItem("milkRate") ?  localStorage.getItem("milkRate") : 1.0;

function prettify(input){
    var output = Math.round(input * 1000000)/1000000;
  return output;
}




$("#milkButton").click(function(e) {

    
    milk += milkRate;
   
    document.getElementById("milk").innerHTML = prettify(milk);
    
});


$( document ).ready(function() {
    

  $("#milkShakeBuy").click(function(e) {

    if (milk >= MSmilkCost){

      milk -= MSmilkCost;
      milkShakes = milkShakes += 1;
      
      document.getElementById("milk").innerHTML = prettify(milk);
      document.getElementById("milkShakes").innerHTML = prettify(milkShakes);
      
      
      
    }
    else{
      pass;
    }
      
  });
});  


$("#iceCreamBuy").click(function(e) {

  if (milk >= ICmilkCost){

    milk -= ICmilkCost;
    iceCreams = iceCreams += 1;
      
    document.getElementById("milk").innerHTML = prettify(milk);
    document.getElementById("iceCreams").innerHTML = prettify(iceCreams);
    
    
  }
  else{
    pass;
  }
    
    
});











function save(){

  var save = {

    milk: milk,
    cash: cash,
    iceCreams: iceCreams,
    milkShakes: milkShakes,
  }
  localStorage.setItem("save",JSON.stringify(save));
};


function load(){
    var savegame = JSON.parse(localStorage.getItem("save"));
    milk = savegame.milk;
    cash = savegame.cash;
    milkShakes = savegame.milkShakes; 
    iceCreams = savegame.iceCreams;
    document.getElementById('milk').innerHTML = milk;
    document.getElementById('cash').innerHTML = cash;
    document.getElementById('milkShakes').innerHTML = milkShakes;
    document.getElementById('iceCreams').innerHTML = iceCreams;
};

window.setInterval(function(){
  save();
  document.getElementById('milk').innerHTML = milk;
  document.getElementById('cash').innerHTML = cash;

  
    
}, 1000);
<div class = "row">
    <div id = "stats"> Milk = <span class = "stat" id = "milk">0.0 <br /> </span></div>
    <div id = "stats">Cash = $<span id = "cash" >0.0</span></div>
          
</div>
<div class = "col" ><button id = "milkButton"class="button">Milk The Cow</button></div>
 

<p>Buy MIlkShake for <span id ="MSmilkCost">0</span>Milk</p>
<button class="button" id = "milkShakeBuy">Buy</button>
<br />
<p>Buy Ice Cream for <span id ="ICmilkCost">0</span>Milk</p>
<button class="button" id = "iceCreamBuy">Buy</button>

0 个答案:

没有答案