How JavaScript implements pause _javascript tips

Source: Internet
Author: User

This article describes the implementation of JS custom Web page Drag class. Share to everyone for your reference, specific as follows:
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:

 <script language= "JavaScript" > Function sleep (obj,iminsecond) {if (Window.eve 
   Ntlist==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 places the function to be paused in the array window.eventlist and calls the continuation 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 (); 
   
  }/* The function calls the NeXTSTEP method of the function being paused, and calls the function again if there is no such method. 
   Once the function has been written, we can do the following: */function Test () {alert ("Hellow"); Sleep (this,3000);/call suspend function this. 
   Nextstep=function () {alert ("NextStep"); 
  } Test (); </script> 

  Hope this article will help you learn about JavaScript programming.

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.