The following is my implementation method:
Code
Copy codeThe Code is as follows: <SCRIPT type = "text/JavaScript">
Function Test ()
{
VaR S = "JavaScript -- pause a function ";
SetTimeout (
Function (){
Eval (sleep (TEST); // The code after the return is executed, that is, "alert (s );"
},
2000
); // Two seconds later
Return; // exit after running
Alert (s );
}
Function sleep (func)
{
If (func = NULL)
{
Return;
}
VaR Reg =/[\ n \ r]/g;
VaR funcstr = func. tostring (). Replace (Reg, ''); // Replace the carriage return and line break.
Reg =/. +? Sleep. +? Return ;/;
Funcstr = '{' + funcstr. Replace (Reg, ''); // remove the code before the test and return functions.
Return funcstr;
}
Test ();
</SCRIPT>
This may have some limitations and cannot be as intuitive and easy to use as the sleep functions in Java and C.
But I think it's quite interesting to share it with you.