在javascript中减少时间
(second.innerHTML)-1可以工作,但是(num.innerHTML)-1不能工作为什么在javascript中减少时间,javascript,Javascript,(second.innerHTML)-1可以工作,但是(num.innerHTML)-1不能工作为什么 function doDcrements() { var hidden = document.getElementById('hidden'); var second = document.getElementById('second'); // the second is 20. var loopTimer = 0; if (second.inner
function doDcrements() {
var hidden = document.getElementById('hidden');
var second = document.getElementById('second'); // the second is 20.
var loopTimer = 0;
if (second.innerHTML != "0") {
second.innerHTML = (second.innerHTML) -1;
second.style.color = "blue";
loopTimer = setTimeout('doDecrements()',1000);
}else {
second.style.color = "grey";
hidden.style.display = "block";
}
}
徖
function doDcrements() {
var hidden = document.getElementById('hidden');
var second = document.getElementById('second'); // the second is 20.
var loopTimer = 0;
var num = document.getElementById('num'); // the number is 20.
if (second.innerHTML != "0") {
second.innerHTML = (num.innerHTML) -1;
second.style.color = "blue";
loopTimer = setTimeout('doDecrements()',1000);
}else {
second.style.color = "grey";
hidden.style.display = "block";
}
}
当我通过for循环创建它时,它不会发生:
function doDcrements() {
var hidden = document.getElementById('hidden');
var second = document.getElementById('second');
for (i=20; i<=0; i--) {
if (second.innerHTML != "0") {
second.innerHTML = i;
second.style.color = "blue";
loopTimer = setTimeout('doDecrements()',1000);
}else {
second.style.color = "grey";
hidden.style.display = "block";
}
}
}
function-doDcrements(){
var hidden=document.getElementById('hidden');
var second=document.getElementById('second');
对于(i=20;i请查看您的for循环:
for (i=20; i<=0; i--)
for(i=20;i(second.innerHTML)-1有效,但(num.innerHTML)-1无效为什么?
在看不到代码的情况下,唯一的猜测是num.innerHTML没有提供可转换为数字的字符串。示例:
'20'-1=20
'-1=NaN
您的HTML没有id=“num”的元素。如果是这种情况,num将为null
变化
setTimeout(doDecrements,1000);
对于(i=20;i>=0;i--)
最好执行setTimeout(doDecrements,1000);
而不是setTimeout('doDecrements()',1000);
你能同时提供你的HTML代码吗?他们可能意味着反之亦然(=
而不是
for (i=20; i<=0; i--)