Javascript变量未使用进度条更新

时间:2016-07-06 18:43:36

标签: javascript html css

我正在制作游戏并使用进度条帮助增加html页面上的某些值。但是,一旦条形图达到100%,它就会更新成本和级别,但它只能运行一次。之后,无论您点击多少次,它都不会更新硬盘级别。如何保持变量更新?

//stats
var cash = document.getElementById('cash');
var ram = document.getElementById('ram');
var internet = document.getElementById('internet');
var hd = document.getElementById('harddrive');
var level = document.getElementById('level');
//shop
function levelup(t){
	var bar = document.getElementById('lvelprogress');
	bar.value = bar.value + t;
	t = 0
	if(t == 100){
		bar.value = 100;
	};
};
function upgradehd(al) { 
	var hdc = document.getElementById('hdc');
	var hdl = document.getElementById('hdl');
	var levelhd = 1;
	var nhd = 250
	var to = level * 20;
	var nhdc = Math.floor(2.5 * hdc.innerHTML); 
	var bar = document.getElementById('progressBar');
	var status = document.getElementById('status');
	status.innerHTML = al+"%";
	bar.value = al;
	document.getElementById('lvelprogress').max = 100;
	al++
	var sim = setTimeout("upgradehd("+al+")",to);
	if(al == 100){
		var xp = hdc.innerHTML / 4;
		levelup(xp);
		status.innerHTML = "100%";
		hdl.innerHTML = levelhd + 1;
		hdc.innerHTML = nhdc;
		hd.innerHTML = nhd + 50;
		al=0;
		status.innerHTML = al+"%";
		bar.value = 0;
		clearTimeout(sim);
	};
};
			.upgrade{
				background-color: #66ff33;
				width: 200px;
			}
			.shop{
				background-color: black;
				color: #66ff33;
				width: 200px;
				border: 5px solid #cccccc
			}
			.stats{
				float: right;
				height: 175px;
				width: 150px;
				background-color: #000;
				color: #66ff33;
				margin-left: 2px;
				text-align: center;
				display: inline-block;
			}
		<div class="stats">
			<p>Level: <span id="level">1</span></p>
			<progress style="width:150px;background-color:#66ff33;" id="lvelprogress" value="0" max="100"></progress>
			<p>Cash: $<span id="cash">50</span></p>
			<p>R.A.M: <span id="ram">100</span>MB</p>
			<p>Internet Speed: <span id="internet">25</span>MB/s</p>
			<P>Hard Drive: <span id="harddrive" >250</span>GB</P>
		</div>
		<div id="shop" class="shop">
			<button class="exit" onclick="hideshop()">X</button>
			<p id="uram">Upgrade R.A.M: $<span id="urc">50</span> Level:<span id="url"> 1</span><button style="border:2px solid white;color:#66ff33;background-color:#000;" onclick="upgradehd(0);">Upgrade</button></p>
			<p id="uis">Upgrade Internet Speed: $<span  id="isc">200</span> Level:<span id="isl"> 1</span><button style="border:2px solid white;color:#66ff33;background-color:#000;" onclick="upgradehd(0);">Upgrade</button></p>
			<p id="uhd">Upgrade Hard Drive: $<span id="hdc">100</span> Level:<span id="hdl"> 1</span><button style="border:2px solid white;color:#66ff33;background-color:#000;" onclick="upgradehd(0);">Upgrade</button></p>
			<progress class="upgrade" id="progressBar" value="0" max="100"></progress>
			<span id="status"></span>
		</div>

1 个答案:

答案 0 :(得分:1)

您始终将LevelValue设置为1. var levelhd = 1;但您必须使用HTML中的当前级别,或者将其保存到JavaScript中的var

我换了线:
var levelhd = parseFloat(hdl.innerHTML);

&#13;
&#13;
//stats
var cash = document.getElementById('cash');
var ram = document.getElementById('ram');
var internet = document.getElementById('internet');
var hd = document.getElementById('harddrive');
var level = document.getElementById('level');
//shop
function levelup(t) {
  var bar = document.getElementById('lvelprogress');
  bar.value = bar.value + t;
  t = 0
  if (t == 100) {
    bar.value = 100;
  };
};

function upgradehd(al) {
  var hdc = document.getElementById('hdc');
  var hdl = document.getElementById('hdl');
  var levelhd = parseFloat(hdl.innerHTML);
  var nhd = 250
  var to = level * 20;
  var nhdc = Math.floor(2.5 * hdc.innerHTML);
  var bar = document.getElementById('progressBar');
  var status = document.getElementById('status');
  status.innerHTML = al + "%";
  bar.value = al;
  document.getElementById('lvelprogress').max = 100;
  al++
  var sim = setTimeout("upgradehd(" + al + ")", to);
  if (al == 100) {
    var xp = hdc.innerHTML / 4;
    levelup(xp);
    status.innerHTML = "100%";
    hdl.innerHTML = levelhd + 1;
    hdc.innerHTML = nhdc;
    hd.innerHTML = nhd + 50;
    al = 0;
    status.innerHTML = al + "%";
    bar.value = 0;
    clearTimeout(sim);
  };
};
&#13;
.upgrade {
  background-color: #66ff33;
  width: 200px;
}
.shop {
  background-color: black;
  color: #66ff33;
  width: 200px;
  border: 5px solid #cccccc
}
.stats {
  float: right;
  height: 175px;
  width: 150px;
  background-color: #000;
  color: #66ff33;
  margin-left: 2px;
  text-align: center;
  display: inline-block;
}
&#13;
<div class="stats">
  <p>Level: <span id="level">1</span>
  </p>
  <progress style="width:150px;background-color:#66ff33;" id="lvelprogress" value="0" max="100"></progress>
  <p>Cash: $<span id="cash">50</span>
  </p>
  <p>R.A.M: <span id="ram">100</span>MB</p>
  <p>Internet Speed: <span id="internet">25</span>MB/s</p>
  <P>Hard Drive: <span id="harddrive">250</span>GB</P>
</div>
<div id="shop" class="shop">
  <button class="exit" onclick="hideshop()">X</button>
  <p id="uram">Upgrade R.A.M: $<span id="urc">50</span> Level:<span id="url"> 1</span>
    <button style="border:2px solid white;color:#66ff33;background-color:#000;" onclick="upgradehd(0);">Upgrade</button>
  </p>
  <p id="uis">Upgrade Internet Speed: $<span id="isc">200</span> Level:<span id="isl"> 1</span>
    <button style="border:2px solid white;color:#66ff33;background-color:#000;" onclick="upgradehd(0);">Upgrade</button>
  </p>
  <p id="uhd">Upgrade Hard Drive: $<span id="hdc">100</span> Level:<span id="hdl"> 1</span>
    <button style="border:2px solid white;color:#66ff33;background-color:#000;" onclick="upgradehd(0);">Upgrade</button>
  </p>
  <progress class="upgrade" id="progressBar" value="0" max="100"></progress>
  <span id="status"></span>
</div>
&#13;
&#13;
&#13;