在使用javascript时,我注意到了这件事。您可以使用
var i=0;
var startingTime=new Date().getTime();
setInterval("foo()",1);
function foo() {
i+=1;
if ($("#foodiv").text()==i) {
//we detected a doubled value (parallel execution)
$("#repdiv").append("[repetition on "+i+"]");
}
$("#foodiv").html(i);
$("#timediv").html(Math.floor((new Date().getTime()-startingTime)/1000));
}
但是当我阅读并尝试自己时,时间不是 1ms ,它至少是 10ms 之类的。事实上,在 10 秒后,我的值 i 约为 2300/2400 ,而不是预期的 10000 。
这是该过程的最小可能时间因素???当然不。如果我试试这个:
<html><head>
<script language="javascript" type="text/javascript" src="jquery-1.4.min.js"></script>
<script type="text/javascript">
var i=0;
var startingTime=new Date().getTime();
setInterval("foo()",1);setInterval("foo()",1);setInterval("foo()",1);
setInterval("foo()",1);setInterval("foo()",1);setInterval("foo()",1);
setInterval("foo()",1);setInterval("foo()",1);setInterval("foo()",1);
setInterval("foo()",1);setInterval("foo()",1);setInterval("foo()",1);
setInterval("foo()",1);setInterval("foo()",1);setInterval("foo()",1);
setInterval("foo()",1);setInterval("foo()",1);setInterval("foo()",1);
setInterval("foo()",1);setInterval("foo()",1);setInterval("foo()",1);
setInterval("foo()",1);setInterval("foo()",1);setInterval("foo()",1);
function foo() {
i+=1;
if ($("#foodiv").text()==i) {
//we detected a doubled value (parallel execution)
$("#repdiv").append("[repetition on "+i+"]");
}
$("#foodiv").html(i);
$("#timediv").html(Math.floor((new Date().getTime()-startingTime)/1000));
}
</script>
</head>
<body>
<div id="foodiv"></div> (counter)
<br/>
<div id="timediv"></div> (seconds passed)
<br/>
<div id="repdiv"></div>
<br/>
</body>
</html>
计数器会走得很快,10 秒后,我的值是 12000 !!!!!!这对我来说是无法解释的,因为调用不是并行执行的(或者至少我们可以为不同的调用获得一些加倍的 i 读取值,在 repdiv div 中计算)。
有人可以解释一下吗?我知道所有这些调用都给 CPU 带来了很大压力,但至少它出人意料地加快了速度。
我阅读了您在论坛中的所有回复和其他任务,他们证实了我的想法。但真正的问题是为什么!为什么他们将限制设置为 15 毫秒,而我可以进行多次连续调用以获得更低的时间?我确信这个多重回调系统不是好的做法,但我可以做到,而且我可能会使 CPU 负载饱和。