我试图做一个网页,当它开始加载,使用一个间隔启动一个计时器。
当页面完全加载时,它停止定时器,
但99%的时间我得到时间测量的0.00或0.01,即使它需要更长的时间。
偶尔,它说某事更有意义,例如.28或3.10在某些时候。
这里是代码如果它有帮助:
var hundredthstimer = 0;
var secondplace = 0;
function addinc(){
hundredthstimer += 1;
if (inctimer == 100){
hundredthstimer = 0;
secondplace += 1;
}
}
var clockint = setInterval(addinc, 10);
function init(){
var bconv1 = document.getElementById("bconverter1");
var bconv2 = document.getElementById("bconverter2");
$(bconv2).hide();
clearInterval(clockint);
if (inctimer.len !== 2){
inctimer = "0" + inctimer;
}
alert(secondplace + "." + inctimer);
}
onload = init;
所以它基本上创建一个称为百分之一的变量,每10毫秒(.01秒)增加1。
然后,如果这个数字达到1000(1完整秒),一个名为secondsplace的变量增加1,因为这是它运行了多少整秒。
然后,它警告秒位置,小数点和百分之一作为总加载时间。
但是上面带有不正确数字的问题仍然存在。为什么?