The sleep function is not available in JavaScript, and we use this feature for the length of time.
One idea is to run a cycle of the body, so that the program consumed CPU time to achieve delay. This has a problem, the different machine CPU execution speed is not the same, this can easily cause slow machine will sleep for a long time.
I see a witty solution from other forums, and the speed of execution is the same on different machines. Share with you here.
Copy Code code as follows:
function sleep (n)
{
var start=new Date (). GetTime ();
while (true) if (new Date (). GetTime ()-start>n) the break;
}
Of course, this method is still rely on idling CPU method.
Another method is to use the settimeout () function.
The function syntax is as follows: SetTimeout (CODE,MILLISEC)
Use Example:
var t=settimeout ("Alert (' 5 seconds! ')", 5000)
The function of this code is to execute code after millisec, in which the alert function is executed after 5000 milliseconds. You can also achieve the same effect as sleep.