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 写类方式之三
Jul 05 Javascript
Javascript 面向对象之重载
May 04 Javascript
JQuery入门——事件切换之hover()方法应用介绍
Feb 05 Javascript
jQuery setTimeout()函数使用方法
Apr 07 Javascript
javascript的push使用指南
Dec 05 Javascript
JS对字符串编码的几种方式使用指南
May 14 Javascript
react-router JS 控制路由跳转实例
Jun 15 Javascript
bootstrap3中container与container_fluid外层容器的区别讲解
Dec 04 Javascript
微信小程序开发之点击按钮退出小程序的实现方法
Apr 26 Javascript
vue 实现搜索的结果页面支持全选与取消全选功能
May 10 Javascript
JavaScript 类的封装操作示例详解
May 16 Javascript
JS如何使用剪贴板操作Clipboard API
May 17 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 代码优化的42条建议 推荐
2009/09/25 PHP
CodeIgniter上传图片成功的全部过程分享
2013/08/12 PHP
linux使用crontab实现PHP执行计划定时任务
2014/05/10 PHP
PHP开发中解决并发问题的几种实现方法分析
2017/11/13 PHP
Laravel获取当前请求的控制器和方法以及中间件的例子
2019/10/11 PHP
Jquery网页出现的乱码问题的三种解决方法
2013/06/30 Javascript
IE8下Jquery获取select选中的值post到后台报错问题
2014/07/02 Javascript
js实现瀑布流的三种方式比较
2020/06/28 Javascript
在Web项目中引入Jquery插件报错的完美解决方案(图解)
2016/09/19 Javascript
jquery checkbox的相关操作总结
2016/10/17 Javascript
Bootstrap页面缩小变形的快速解决办法
2017/02/03 Javascript
easyui简介_动力节点Java学院整理
2017/07/14 Javascript
nginx部署访问vue-cli搭建的项目的方法
2018/02/12 Javascript
Vue2.0子同级组件之间数据交互方法
2018/02/28 Javascript
浅谈js获取ModelAndView值的问题
2018/03/28 Javascript
vue鼠标移入添加class样式,鼠标移出去除样式(active)实现方法
2018/08/22 Javascript
js代码规范之Eslint安装与配置详解
2018/09/08 Javascript
vue请求本地自己编写的json文件的方法
2019/04/25 Javascript
python用户评论标签匹配的解决方法
2018/05/31 Python
python实现遍历文件夹修改文件后缀
2018/08/28 Python
pycharm打开命令行或Terminal的方法
2019/01/16 Python
Python判断对象是否为文件对象(file object)的三种方法示例
2019/04/26 Python
python代码打印100-999之间的回文数示例
2019/11/24 Python
Python selenium文件上传下载功能代码实例
2020/04/13 Python
基于python图书馆管理系统设计实例详解
2020/08/05 Python
Pycharm安装python库的方法
2020/11/24 Python
call在Python中改进数列的实例讲解
2020/12/09 Python
Swanson中国官网:美国斯旺森健康产品公司
2021/03/01 全球购物
公务员的自我鉴定
2013/10/26 职场文书
专业求职信撰写要诀
2014/02/18 职场文书
党员岗位承诺书
2014/03/25 职场文书
庆七一活动总结
2014/08/27 职场文书
拾金不昧表扬信
2015/01/16 职场文书
2015年小学总务工作总结
2015/07/21 职场文书
浅谈Python数学建模之线性规划
2021/06/23 Python
码云(gitee)通过git自动同步到阿里云服务器
2022/12/24 Servers