【发布时间】:2020-04-12 17:45:50
【问题描述】:
我有以下代码,应该每秒增加 2.6。但在第一次添加后,我得到 8。 我怎样才能解决这个问题?在这里的测试仪上它可以工作,但在我的页面上我得到 8 https://260044-5.web1.fh-htwchur.ch/
提前致谢。
setTimeout(start, 0000);
var i = 2.6;
var num = document.getElementById('humans');
function start() {
setInterval(increase, 1000);
}
function increase() {
if (i < 100000000) {
i += 2.6;
num.innerText = Math.round(i);
}
}
<div id="humans">2.6</div>
<p>Menschen wurden geboren.</p>
【问题讨论】:
-
把这个
num.innerText = Math.round(i);改成num.innerText = i; -
如果要添加浮动,
Math.round()的目的是什么?
标签: javascript html counter