SetTimeout is often mixed with setinterval, which is commonly used to call a function on a regular basis, such as a simple setinterval clock in "JavaScript" a dynamic time synchronized to local time (click the Open link). You can also write the following code:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
The effect is as follows:
But the actual meaning of settimeout, but not many people pay attention to,
In essence, the following function is executed after n milliseconds, such as the following code:
<script>settimeout ("Test ()"), Alert ("OK"), function test () {alert ("After 5 seconds ...");} </script>
Test () This function is scheduled to execute after 5 seconds, and then the program reads continuously, so the effect is as follows:
So settimeout can make the same effect as setinterval,
is because setinterval sets the XX function to execute every n milliseconds. So, to implement a time-specific effect, SetInterval is written out, and the page starts with a thread that executes the code every n milliseconds.
SetTimeout is written in the function, the first is called once, and then after n milliseconds after the set call itself, n milliseconds after the call itself, the task is completed. But you also have an n-millisecond call to your task. Therefore, in turn the cycle of endless, but also to achieve the effect of setinterval.
"JavaScript" settimeout and SetInterval