我的循环表现有些问题很有趣,这里是:
var duration = 1000; /* 1000 millisecond fade = 1 sec */
var steps = 20; /* number of opacity intervals */
var delay = 5000; /* 5 sec delay before fading out */
function fadeOut(eid) {
for (i = 0; i <= 1; i += (1 / steps)) {
setTimeout("setOpacity(" + (1 - i) + ",'"+eid+"')", i * duration);
console.log("i="+i);
}
}
我使用console.log检查循环中发生了什么,结果非常奇怪:
i=0
i=0.05
i=0.1
i=0.15000000000000002
i=0.2
i=0.25
i=0.3
i=0.35
i=0.39999999999999997
i=0.44999999999999996
i=0.49999999999999994
i=0.5499999999999999
i=0.6
i=0.65
i=0.7000000000000001
i=0.7500000000000001
i=0.8000000000000002
i=0.8500000000000002
i=0.9000000000000002
i=0.9500000000000003
答案 0 :(得分:2)
这些是实际的浮点值。您可能希望将它们舍入以用于显示目的。
console.log("i="+i.toFixed(2));