我遇到了这个脚本的问题:
<script>
var start = 400;
var interval = 40;
function counter() {
return start -= interval;
}
var stop = setInterval (
function add() {
if (counter() > 0)
document.getElementById("test").innerHTML = counter();
else
clearInterval(stop);
},1000);
</script>
<button onclick="clearInterval(stop)">stop!</button>
<br/>
<p id="test">On marks! Start!</p>
脚本从400倒数到0,变量间隔= 40.当脚本运行时,它减去80而不是40.结果是双倍,我不知道为什么。
你能帮助我吗?
答案 0 :(得分:3)
你两次调用counter(),每次减去40,只调用一次
var start = 400;
var interval = 40;
function counter() {
return start -= interval;
}
var stop = setInterval(function() {
var count = counter();
if (count > 0) {
document.getElementById("test").innerHTML = count;
} else {
clearInterval(stop);
}
}, 1000);