js 链式延迟执行DOME


Posted in Javascript onJanuary 04, 2012

这样的形式执行:

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>
Javascript 相关文章推荐
javascript new后的constructor属性
Aug 05 Javascript
javascript改变position值实现菜单滚动至顶部后固定
Jan 18 Javascript
点击隐藏页面左栏或右栏实现js代码
Apr 01 Javascript
js完美的div拖拽实例代码
Jan 22 Javascript
Node.js异步I/O学习笔记
Nov 04 Javascript
javascript实现3D切换焦点图
Oct 16 Javascript
Extjs表单输入框异步校验的插件实现方法
Mar 20 Javascript
JavaScript实现重力下落与弹性效果的方法分析
Dec 20 Javascript
JS中的算法与数据结构之二叉查找树(Binary Sort Tree)实例详解
Aug 16 Javascript
node 文件上传接口的转发的实现
Sep 23 Javascript
javascript实现计算器功能详解流程
Nov 01 Javascript
javascript之Object.assign()的痛点分析
Mar 03 Javascript
常见效果实现之返回顶部(结合淡入、淡出、减速滚动)
Jan 04 #Javascript
Textarea与懒惰渲染实现代码
Jan 04 #Javascript
js中有关IE版本检测
Jan 04 #Javascript
国外大牛IE版本检测!现在IE都到9了,IE检测代码
Jan 04 #Javascript
IE的有条件注释判定IE版本详解(附实例代码)
Jan 04 #Javascript
js变量以及其作用域详解
Jul 18 #Javascript
分享一个asp.net pager分页控件
Jan 04 #Javascript
You might like
php获取文章上一页与下一页的方法
2014/12/01 PHP
php自定义分页类完整实例
2015/12/25 PHP
CI框架的安全性分析
2016/05/18 PHP
JS实现打开本地文件或文件夹
2021/03/09 Javascript
dwr spring的集成实现代码
2009/03/22 Javascript
jQuery 工具函数学习资料
2010/04/29 Javascript
window.dialogArguments 使用说明
2011/04/11 Javascript
谈谈关于JavaScript 中的 MVC 模式
2013/04/11 Javascript
js 输出内容到新窗口具体实现代码
2013/05/31 Javascript
原生Javascript封装的一个AJAX函数分享
2014/10/11 Javascript
jQuery()方法的第二个参数详解
2015/04/29 Javascript
js实现(全选)多选按钮的方法【附实例】
2016/03/30 Javascript
JS实现图片延迟加载并淡入淡出效果的简单方法
2016/08/25 Javascript
Bootstrap模态对话框用法简单示例
2018/08/31 Javascript
vue prop属性传值与传引用示例
2019/11/13 Javascript
让你30分钟快速掌握vue3教程
2020/10/26 Javascript
javascript实现下拉菜单效果
2021/02/09 Javascript
Django与遗留的数据库整合的方法指南
2015/07/24 Python
Python六大开源框架对比
2015/10/19 Python
Python利用前序和中序遍历结果重建二叉树的方法
2016/04/27 Python
Django 多环境配置详解
2019/05/14 Python
python的几种矩阵相乘的公式详解
2019/07/10 Python
Python学习笔记之For循环用法详解
2019/08/14 Python
在Python中等距取出一个数组其中n个数的实现方式
2019/11/27 Python
将tensorflow模型打包成PB文件及PB文件读取方式
2020/01/23 Python
在python中使用pyspark读写Hive数据操作
2020/06/06 Python
浅析python 字典嵌套
2020/09/29 Python
python链表类中获取元素实例方法
2021/02/23 Python
实现CSS3中的border-radius(边框圆角)示例代码
2013/07/19 HTML / CSS
学生会竞选自荐信
2013/10/12 职场文书
春季运动会广播稿大全
2014/02/19 职场文书
聘任书模板
2014/03/29 职场文书
国际贸易毕业生自荐书
2014/06/22 职场文书
农林经济管理专业自荐信
2014/09/01 职场文书
开国大典观后感
2015/06/04 职场文书
关于Python OS模块常用文件/目录函数详解
2021/07/01 Python