The following code uses JS to pause and continue the program
Copy codeThe Code is as follows:
<Script type = "text/javascript">
/* Implementation of the pause function in Javascript
Javascript itself does not have the pause function (sleep cannot be used) and vbscript cannot use doEvents. Therefore, write this function to implement this function.
Javascript is a weak object language, and a function can also be used as an object.
For example:
Function Test (){
Alert ("hellow ");
This. NextStep = function (){
Alert ("NextStep ");
}
}
We can call var myTest = new Test (); myTest. NextStep ();
When we pause a function, we can divide it into two parts, which are unchanged before the pause operation and put the code to be executed after the pause in this. NextStep.
To control the pause and continue, we need to write two functions to implement the pause and continue functions respectively.
The pause function is as follows:
*/
Function Pause (obj, iMinSecond ){
If (window. eventList = null) window. eventList = new Array ();
Var ind =-1;
For (var I = 0; I <window. eventList. length; I ++ ){
If (window. eventList [I] = null ){
Window. eventList [I] = obj;
Ind = I;
Break;
}
}
If (ind =-1 ){
Ind = window. eventList. length;
Window. eventList [ind] = obj;
}
SetTimeout ("GoOn (" + ind + ")", iMinSecond );
}
/*
This function puts the function to be paused in the Array window. eventList, and calls the continue function through setTimeout.
The continuation function is as follows:
*/
Function GoOn (ind ){
Var obj = window. eventList [ind];
Window. eventList [ind] = null;
If (obj. NextStep) obj. NextStep ();
Else obj ();
}
/*
Call the NextStep method of the suspended function. If this method is not available, call the function again.
After the function is compiled, we can perform the following operations:
*/
Function Test (){
Alert ("hellow ");
Pause (this, 3000); // call the Pause function
This. NextStep = function (){
Alert ("NextStep ");
}
}
</Script>