Method 1: This is a method found online. It can be used. But to be honest, I don't quite understand this method... Well written and complex. What is the difference between this and setTimeout?
Copy codeThe Code 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 );
}
Function GoOn (ind ){
Var obj = window. eventList [ind];
Window. eventList [ind] = null;
If (obj. NextStep) obj. NextStep ();
Else obj ();
}
Function testJsStop (){
Alert ("1 ");
Pause (this, 3000 );
This. NextStep = function (){
Alert ("2 ");
}
}
Method 2: This is also found online and can be used. The principle is to bring up a window first, because the code will be paused at the current position during the JS pop-up window. Wait for a while before executing the close window function, and the code continues to be executed. This method is very simple, but what is annoying is that it will pop up a window...Copy codeThe Code is as follows: function pause (numberMillis ){
Addcloud ();
Var dialogScript = 'window. setTimeout ('+' function () {$ ("# bgDiv"). remove () ;}, '+ numberMillis + ');';
Var result = window. showModalDialog ('javascript: document. writeln ('+' "<script> '+ dialogScript +' <'+'/script> ")');
}
Function test (){
Var a = 0;
Alert ();
Pause (5000 );
A = 999;
Alert ();
}
Method 3: I wrote this method myself. Because the functions I want to implement are complex, we need to call the getpath () method cyclically. The previous two methods can only be applied to code segments that are executed in sequence and cannot control loops. Here I use the combination of front and back-end methods. In the foreground, call the background method through Ajax and directly suspend the thread for 1 s, thus implementing Force suspension of JS Code.
Front-end JS:Copy codeThe Code is as follows: function getpath (){
Var time = 1000;
$. AjaxSettings. async = false;
$. GetJSON ("../Actions/TspHandler. ashx? RKey = "+ parseInt (Math. random () * 999 + 1). toString () +" & opKey = Sleep"
+ "& Time =" + time,
Null,
Function (json ){
});
..........
}
Background ashx:Copy codeThe Code is as follows: if (methodname = "Sleep") // Sleep
{
Int time = int. Parse (req ["Time"]. ToString ());
System. Threading. Thread. Sleep (time );
}
The above is for your reference only!