Does this setInterval multithreading behavior mean in Javascript?

In using javascript, I noticed this thing. you can use

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));
}

but when I read and try myself, the time is not 1 ms, it is at least 10 ms or something like that. In fact, after 10 seconds, I have a value of about 2300/2400, and not 10000, as expected.

Is this the smallest possible time factor for the procedure ??? required NO. If I try this:

<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>

The counter will be very fast, and after 10 seconds I will have a value of 12000 !!!!. This is inexplicable to me, because the call is not executed in parallel (or, at least, we could have several doubled values ​​for different calls, appearing in a div div).

- ? , , , , .

, . : ! 15 , , ? , , , .

+5
5

, Javascript . setInterval setTimeout , . , , , , , , . , 12 , . , 1 . 15 (. .) ( setInterval, . , , ).

, , 15 . , . , 15 , .

+8

JavaScript 15 , . AFAIK Google Chrome 4 . . " " (setInterval delay) JavaScript?.

+2

, JavaScript , , .

, , setInterval.

+1

, , , :

var i=0; 
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);
}

, , , , , . , .

( , . 200 , , 100% .)

- , , , , . , , 15 .

+1

, setInterval, . 1 1 , , . , , , 15 .

, , , .

. setInterval, , . 1, . , , ( , 1 ).

0
source

All Articles