DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> js 鏈式延遲執行DOME
js 鏈式延遲執行DOME
編輯:關於JavaScript     
這樣的形式執行:
復制代碼 代碼如下:
d.wait(3000).run(function(m){ //等待3秒
alert('從前有座山');
}).wait(1000).run(function(m){ //等待1秒
alert('山裡有座廟');
}).wait(2000).run(function(m){ //等待2秒
alert('廟裡有一個老和尚給一個小和尚講故事');
}).wait(3000).run(function(m){ //等待3秒
alert('講的故事是:');
}).goStart(); //跳到第一條重新執行,這句拿掉就只執行一次


復制代碼 代碼如下:
<!doctype html>
<html>
<head>
<title>異步執行</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('從前有座山');
}).wait(2000).run(function(m){
alert('山裡有座廟');
}).wait(2000).run(function(m){
alert('廟裡有一個老和尚給一個小和尚講故事');
}).wait(2000).run(function(m){
alert('講的故事是:');
}).goStart();
</script></body>
</html>
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved