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

JS implementation of program pause and continue function code interpretation_javascript skills

WBOY
Release: 2016-05-16 17:20:33
Original
1056 people have browsed it

The following code uses JS to pause and continue the program

Copy the code The code is as follows:

< ;script type="text/javascript">
/*Implementation of pause function in Javascript
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");
}
}
We can call var myTest=new Test();myTest.NextStep();


When we pause, we can divide a function into two parts, before the pause operation No change, put the code to be executed after the pause in this.NextStep.
In order to control pause and resume, we need to write two functions to implement the pause and resume 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;iif (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 sets the function to be paused Put it into the array window.eventList, and call the continuation function through setTimeout.


The continued function is as follows:
*/


function GoOn(ind){
var obj=window.eventList[ind];
window .eventList[ind]=null;
if (obj.NextStep) obj.NextStep();
else obj();
}
/*
This function calls the suspended function NextStep method, if there is no such method, the function will be called again.


After the function is written, we can do the following:
*/
function Test(){
alert("hellow");
Pause(this, 3000);//Call the pause function


this.NextStep=function(){
alert("NextStep");
}
}
Related labels:
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