Prototype PeriodicalExecuter对象 学习


Posted in Javascript onJuly 19, 2009

This is a simple facility for periodical execution of a function. This essentially encapsulates the native clearInterval/setInterval mechanism found in native Window objects.

This is especially useful if you use one to interact with the user at given intervals (e.g. use a prompt or confirm call): this will avoid multiple message boxes all waiting to be actioned.

这个对象就是可以周期性的执行某个方法,但是在它内部维持了一个状态,可以防止由于某些原因一次调用没执行,然后下一次调用又来了,这样会造成连续执行两次方法。上面的第二断英文就是这个意思。

帮助文档上说这个对象只提供了一个方法stop,但是在我看的源码里还提供了一个事件onTimerEvent,应该可以在某个时候触发这个事件。但帮助文档上没有给出示例。

这个对象源码比较简单,这里直接贴出来了,就不再注释了:

var PeriodicalExecuter = Class.create({ 
initialize: function(callback, frequency) { 
this.callback = callback; 
this.frequency = frequency; 
this.currentlyExecuting = false; this.registerCallback(); 
}, 
registerCallback: function() { 
this.timer = setInterval(this.onTimerEvent.bind(this), this.frequency * 1000); 
}, 
execute: function() { 
this.callback(this); 
}, 
stop: function() { 
if (!this.timer) return; 
clearInterval(this.timer); 
this.timer = null; 
}, 
onTimerEvent: function() { 
if (!this.currentlyExecuting) { 
try { 
this.currentlyExecuting = true; 
this.execute(); 
} catch(e) { 
/* empty catch for clients that don't support try/finally */ 
} 
finally { 
this.currentlyExecuting = false; 
} 
} 
} 
});

看一下示例:
new PeriodicalExecuter(function(pe) { 
if (!confirm('Want me to annoy you again later?')) 
pe.stop(); }, 
5); 
// Note that there won't be a stack of such messages if the user takes too long 
// answering to the question...
Javascript 相关文章推荐
JavaScript 数组的 uniq 方法
Jan 23 Javascript
纯文字版返回顶端的js代码
Aug 01 Javascript
js监听鼠标事件控制textarea输入字符串的个数
Sep 29 Javascript
jQuery Ajax()方法使用指南
Nov 19 Javascript
javascript格式化日期时间方法汇总
Jun 19 Javascript
跟我学习JScript的Bug与内存管理
Nov 18 Javascript
使用struts2+Ajax+jquery验证用户名是否已被注册
Mar 22 Javascript
Vue中添加过渡效果的方法
Mar 16 Javascript
JS数组去重(4种方法)
Mar 27 Javascript
VUE 3D轮播图封装实现方法
Jul 03 Javascript
使用svg实现动态时钟效果
Jul 17 Javascript
layer页面跳转,获取html子节点元素的值方法
Sep 27 Javascript
Prototype String对象 学习
Jul 19 #Javascript
Prototype Template对象 学习
Jul 19 #Javascript
Prototype Number对象 学习
Jul 19 #Javascript
Prototype ObjectRange对象学习
Jul 19 #Javascript
Prototype RegExp对象 学习
Jul 19 #Javascript
Prototype Class对象学习
Jul 19 #Javascript
javascript iframe内的函数调用实现方法
Jul 19 #Javascript
You might like
php下保存远程图片到本地的办法
2010/08/08 PHP
php集成套件服务器xampp安装使用教程(适合第一次玩PHP的新手)
2015/06/03 PHP
php 如何获取文件的后缀名
2016/06/05 PHP
Yii框架弹出窗口组件CJuiDialog用法分析
2017/01/07 PHP
javascript 动态修改样式和层叠样式表代码
2010/04/27 Javascript
图片img的src不变让浏览器重新加载实现方法
2013/03/29 Javascript
jquery中event对象属性与方法小结
2013/12/18 Javascript
js利用prototype调用Array的slice方法示例
2014/06/09 Javascript
教你如何终止JQUERY的$.AJAX请求
2016/02/23 Javascript
Bootstrap3.0学习教程之JS折叠插件
2016/05/27 Javascript
jQuery Validate验证框架详解(推荐)
2016/12/17 Javascript
jQuery学习之DOM节点的插入方法总结
2017/01/22 Javascript
利用js查找数组中指定元素并返回该元素的所有索引示例
2017/03/29 Javascript
css和js实现弹出登录居中界面完整代码
2017/11/26 Javascript
分析JavaScript数组操作难点
2017/12/18 Javascript
jquery实现企业定位式导航效果
2018/01/01 jQuery
vue.js系列中的vue-fontawesome使用
2018/02/10 Javascript
详解JavaScript函数callee、call、apply的区别
2019/03/08 Javascript
解决 viewer.js 动态更新图片导致无法预览的问题
2019/05/14 Javascript
atom-design(Vue.js移动端组件库)手势组件使用教程
2019/05/16 Javascript
es6函数之尾递归用法实例分析
2020/04/25 Javascript
vue组件开发之slider组件使用详解
2020/08/21 Javascript
ES5和ES6中类的区别总结
2020/12/21 Javascript
[01:07:02]DOTA2-DPC中国联赛 正赛 iG vs PSG.LGD BO3 第三场 2月26日
2021/03/11 DOTA
利用python批量检查网站的可用性
2016/09/09 Python
使用python判断你是青少年还是老年人
2018/11/29 Python
如何将你的应用迁移到Python3的三个步骤
2019/12/22 Python
python logging 日志的级别调整方式
2020/02/21 Python
Python3如何判断三角形的类型
2020/04/12 Python
Python实现网络聊天室的示例代码(支持多人聊天与私聊)
2021/01/27 Python
关于 HTML5 的七个传说小结
2012/04/12 HTML / CSS
文明青少年标兵事迹材料
2014/01/28 职场文书
2015年度党风廉政建设工作情况汇报
2015/01/02 职场文书
2015政治思想表现评语
2015/03/25 职场文书
无罪辩护词范文
2015/05/21 职场文书
HTML中table表格拆分合并(colspan、rowspan)
2021/04/07 HTML / CSS