Execute in the following format:
Copy codeThe Code is as follows:
D. wait (3000). run (function (m) {// wait 3 seconds
Alert ('mountain previously ');
}). Wait (1000). run (function (m) {// wait 1 second
Alert ('Temple in the mountains ');
}). Wait (2000). run (function (m) {// wait 2 seconds
Alert ('there is an old monk in the temple telling a story to a monk ');
}). Wait (3000). run (function (m) {// wait 3 seconds
Alert ('the story is :');
}). GoStart (); // jump to the first line and execute again. If this sentence is removed, it will be executed only once.
Copy codeThe Code is as follows:
<! Doctype html>
<Html>
<Head>
<Title> asynchronous execution </title>
</Head>
<Body>
<Script type = "text/javascript">
Function Delay (){
This. funList = [];
This. index = 0;
This. re = null;
This. isloop = false;
};
Delay. prototype = {
Wait: function (m ){
If (this. funList [this. index] & typeof this. funList [this. index]. fun! = 'Function '){
M + = this. funList [this. index]. m;
}
This. funList [this. index] = {m: m, fun: null };
Return this;
},
Run: function (fun ){
If (typeof this. funList [this. index]. fun! = 'Function '){
This. funList [this. index]. fun = fun;
This. index ++;
} Else {
This. index ++;
This. funList [this. index] = {'M': 0, 'fun': fun };
}
This. start ();
Return this;
},
Start: function (){
Var self = this;
If (this. re) return;
Var setOutrun = function (funList, index ){
If (funList [index] = undefined ){
ClearTimeout (self. re );
Return false;
}
Var m = funList [index]. m,
Fun = funList [index]. fun;
Typeof fun = 'function' | (fun = function (){});
Self. re = setTimeout (function (){
If (fun (index) ===false) return false;
If (self. isloop ){
Index =-1;
Self. isloop = false;
}
SetOutrun (funList, ++ index );
}, M );
}
SetOutrun (this. funList, 0 );
},
Stop: function (){
Return clearTimeout (this. re );
},
GoStart: function (){
Var self = this,
Fun = function (){
Self. isloop = true;
};
If (this. funList [this. index] & typeof this. funList [this. index]. fun! = 'Function '){
This. funList [this. index]. fun = fun;
This. index ++;
} Else {
This. funList [this. index] = {'M': 0, 'fun': fun };
}
This. start ();
}
};
Var d = new Delay ();
D. wait( 3000). run (function (m ){
Alert ('mountain previously ');
}). Wait (2000). run (function (m ){
Alert ('Temple in the mountains ');
}). Wait (2000). run (function (m ){
Alert ('there is an old monk in the temple telling a story to a monk ');
}). Wait (2000). run (function (m ){
Alert ('the story is :');
}). GoStart ();
</Script> </body>
</Html>