源码网,源码论坛,源码之家,商业源码,游戏源码下载,discuz插件,棋牌源码下载,精品源码论坛

 找回密码
 立即注册
楼主: ttx9n

[JavaScript] js 链式延迟执行DOME

[复制链接]

7万

主题

861

回帖

32万

积分

论坛元老

Rank: 8Rank: 8

积分
329525
发表于 2012-1-4 17:33:22 | 显示全部楼层 |阅读模式
js 链式延迟执行DOME,需要的朋友可以参考下。 这样的形式执行:
复制代码 代码如下:
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>
回复

使用道具 举报

1

主题

2万

回帖

55

积分

注册会员

Rank: 2

积分
55
发表于 2022-8-23 12:51:32 | 显示全部楼层
谢谢小Y分享
回复 支持 反对

使用道具 举报

0

主题

2万

回帖

55

积分

注册会员

Rank: 2

积分
55
发表于 2022-10-18 03:26:11 | 显示全部楼层
的谁vdvdsvdsvdsdsv
回复 支持 反对

使用道具 举报

5

主题

2万

回帖

183

积分

注册会员

Rank: 2

积分
183
发表于 2022-10-23 12:05:55 | 显示全部楼层
加快速度很快就撒谎
回复 支持 反对

使用道具 举报

0

主题

2万

回帖

61

积分

注册会员

Rank: 2

积分
61
发表于 2022-12-13 09:24:29 | 显示全部楼层
强烈支持楼主ing……
回复 支持 反对

使用道具 举报

0

主题

1万

回帖

0

积分

中级会员

Rank: 3Rank: 3

积分
0
发表于 2022-12-16 14:44:13 | 显示全部楼层
天天源码论坛
回复 支持 反对

使用道具 举报

1

主题

2万

回帖

319

积分

中级会员

Rank: 3Rank: 3

积分
319
发表于 2023-1-22 22:57:07 | 显示全部楼层
老大你好你好好你好
回复 支持 反对

使用道具 举报

0

主题

1万

回帖

0

积分

中级会员

Rank: 3Rank: 3

积分
0
发表于 2023-2-14 18:51:09 | 显示全部楼层
2222222222222222
回复 支持 反对

使用道具 举报

2

主题

2万

回帖

221

积分

中级会员

Rank: 3Rank: 3

积分
221
发表于 2023-3-10 00:57:35 | 显示全部楼层
1312315458748777
回复 支持 反对

使用道具 举报

6

主题

2万

回帖

425

积分

中级会员

Rank: 3Rank: 3

积分
425
发表于 2023-5-21 08:45:53 | 显示全部楼层
需要很久了终于找到了
回复 支持 反对

使用道具 举报

高级模式
B Color Image Link Quote Code Smilies

本版积分规则

手机版|小黑屋|网站地图|源码论坛 ( 海外版 )

GMT+8, 2025-2-6 18:21 , Processed in 0.066040 second(s), 22 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表