About how to implement JavaScript code Suspension

Source: Internet
Author: User

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.