The implementation of pause function in JavaScript

Source: Internet
Author: User
Tags array object continue window
Javascript

<script language= "JavaScript" >
Implementation of pause function in/*javascript
JavaScript does not have a pause function (sleep cannot be used) while VBScript cannot use DoEvents, so writing this function implements this function.
JavaScript as a weak object language, a function can also be used as an object.
Like what:
function Test () {
Alert ("Hellow");
This. Nextstep=function () {
Alert ("NextStep");
}
}
We can call Var mytest=new Test (); Mytest.nextstep ();

We can do it when we're done. A function is divided into two parts, the same before the pause operation, put the code to be executed after the pause in this. In NeXTSTEP.
To control pause and continue, we need to write two functions to implement the pause and continue function separately.
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);
}
/*
The function places the function to be paused in the array window.eventlist, while the continuation function is called through the 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 ();
}
/*
The function calls the NeXTSTEP method of the function being paused, and calls the function again if there is no such method.

  
After the function is written, we can make the following book:
*/
function Test () {
Alert ("Hellow");
Pause (this,1000),/call suspend 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.