Home > Web Front-end > JS Tutorial > body text

How to implement pause function in javascript

PHPz
Release: 2018-09-28 16:26:29
Original
2971 people have browsed it

This article mainly introduces how to implement the pause function in javascript. Javascript itself does not have a pause function. At the same time, vbscript cannot use doEvents, so this function is written to implement this function. Friends who need it can refer to it. The details are as follows:

Javascript itself does not have a pause function (sleep cannot be used) and vbscript cannot use doEvents, so this function is written 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"); 
   } 
  }
Copy after login

We can call like this var myTest=new Test();myTest.NextStep();

When we pause, we can divide a function into two parts. The code before the pause operation remains unchanged, and the code to be executed after the pause is placed in this.NextStep.

In order to control 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); 
  } 
  /* 
  该函数把要暂停的函数放到数组window.eventList里,同时通过setTimeout来调用继续函数。 
  继续函数如下: 
  */ 
 
  function goon(ind){ 
   var obj=window.eventList[ind]; 
   window.eventList[ind]=null; 
   if (obj.NextStep) obj.NextStep(); 
   else obj(); 
  } 
  /* 
  该函数调用被暂停的函数的NextStep方法,如果没有这个方法则重新调用该函数。 
   
  函数编写完毕,我们可以作如下: 
  */ 
  function Test(){ 
   alert("hellow"); 
   sleep(this,3000);//调用暂停函数 
   this.NextStep=function(){ 
   alert("NextStep"); 
   } 
  } 
Test(); 
  </script>
Copy after login

The above is the entire content of this chapter. For more related tutorials, please visit JavaScript Video Tutorial!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!