更改数字的结尾

时间:2016-12-06 23:14:20

标签: javascript

我的脚本遇到了一些严重问题...由于某种原因,控制台没有和我说话而我的输出没有出现......我在这里要做的是将输出输出到当数量达到1000万时,千万当数量达到1000000并且一直到Quintilian。请帮忙!

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<p id="output"></p>
<button onclick="collectWood()" id="woodButton">collect wood</button>
<script>
window.addEventListener("DOMcontentLoaded", function(){
var wood = +localStorage.getItem("woodSave");
var woodOut = document.getElementById("output");
var woodbtn = document.getElementById('woodButton');

woodThousand();
woodMillion();
woodBillion();
woodTrillion();
woodQuadrillion();
woodQuintillion();

woodbtn.addEventListener("click", collectWood);

function collectWood() {
	wood +=1;
	woodOut.innerHTML = wood;
	localStorage.setItem("woodSave", wood);
	woodThousand();
	woodMillion();
	woodBillion();
	woodTrillion();
	woodQuadrillion();
	woodQuintillion();
}
function woodThousand() {
	console.log(wood);
	woodOut.textContent = (wood >= 1000) ? (wood / 1000).toFixed(2) + "Thousand":wood;
}
function woodMillion() {
	woodOut.textContent = (wood >= 1000000) ? (wood / 1000000).toFixed(2) + "Million":wood;
}
function woodBillion() {
	woodOut.textContent = (wood >= 1000000000) ? (wood / 1000000000).toFixed(2) + "Billion":wood;
}
function woodTrillion() {
	woodOut.textContent = (wood >= 1000000000000) ? (wood / 1000000000000).toFixed(2) + "Trillion":wood;
}
function woodQuadrillion() {
	woodOut.textContent = (wood >= 1000000000000000) ? (wood / 1000000000000000).toFixed(2) + "Quadrillion":wood;
}
function woodQuintillion() {
	woodOut.textContent = (wood >= 1000000000000000000) ? (wood / 1000000000000000000).toFixed(2) + "Quintillion":wood;
}
setInterval(function() { 
woodThousand();
woodMillion();
woodBillion();
woodTrillion();
woodQuadrillion();
woodQuintillion();
}, 1);
});
</script>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

localStorage无法使用SO,因此请参阅 this Fiddle 以获取解决方案。您的代码存在一些问题:

首先它是DOMContentLoaded(大写字母“C”)。这就是页面启动时没有任何反应的原因。其次,您不需要onclick="collectWood(),因为这是在JavaScript中处理的。

可能最大的变化是你不需要为木材计数达到的每个等级都有不同的功能。一个函数可以做(参见小提琴中的if / then)。