当前位置: 首页 > 图文教程 > 网络编程 > Javascript > Prototype PeriodicalExecuter对象 学习

Javascript
javascript 动态数据下的锚点错位问题解决方法
jQuery 动画基础教程
jQuery 操作XML入门
ASP SQL防注入的方法
jquery 插件 web2.0分格的分页脚本,可用于ajax无刷新分页
jquery 插件 任意位置浮动固定层
JavaScript 检测浏览器和操作系统的脚本
不要小看注释掉的JS 引起的安全问题
js实现的验证,学习用js控制td
javascript iframe中打开文件,并检测iframe存在否
Javascript typeof 用法
Javascript valueOf 使用方法
extjs form textfield的隐藏方法
extjs grid取到数据而不显示的解决
Ext第一周 史上最强学习笔记---GridPanel(基础篇)
My Desktop :) 桌面式代码
javascript 双击文本框编辑功能代码
不用写JS也能使用EXTJS视频演示
js 提取class相同的节点集合
js Li来实现的效果

Javascript 中的 Prototype PeriodicalExecuter对象 学习


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-12   浏览: 215 ::
收藏到网摘: n/a

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