Use Javascript to implement the Sleep pause function code

Source: Internet
Author: User

Copy codeThe Code is as follows:
<Script language = "JavaScript">
<! --
Function Sleep (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 Test ()
{
Alert ("sleep ");
Sleep (this, 10000 );
This. NextStep = function ()
{
Alert ("continue ");
}
}

Test ();
// -->
</SCRIPT>

Below is the code written by other netizens
Copy codeThe Code is as follows:
<Script language = "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 + ")", 1000 );
}
/*
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, 1000); // call the Pause function
This. NextStep = function (){
Alert ("NextStep ");
}
}
</Script>

Related Article

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.