How does javascript pause and javascript pause?

Source: Internet
Author: User

How does javascript pause and javascript pause?

This example describes the custom webpage drag class implemented by JS. We will share this with you for your reference. The details are as follows:
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 callVar 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:

<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);}/* put the function to be paused in the Array window. event List, and call the continue function through setTimeout. The continue 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 writing the function, we can do the following: */function Test () {alert ("hellow"); sleep (this, 3000); // call the pause function this. nextStep = function () {alert ("NextStep") ;}} Test (); </script>

I hope this article will help you learn about javascript programming.

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.