Prototype PeriodicalExecuter对象 学习
作者:bea
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 use
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...
有用 | 无用
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 IFrame 强制刷新代码
- jQuery TextBox自动完成条
- JavaScript 动态生成方法的例子
- JavaScript 就地编辑HTML节点实现代码
- 模仿jQuery each函数的链式调用
- js 代码集(学习js的朋友可以看下)
- javascript Base类 包含基本的方法
- 从基础开始建立一个JS代码库
- javascript FAQ函数(提问+回复)
- javascript 表单规则集合对象
- javascript ImgBox透明遮罩层背景图片展示
- JavaScript 进度条实现代码(Firefox等相似浏览器下不支持)
- javascript 三级下拉选择菜单Levels对象
- 一个可以随意添加多个序列的tag函数
- 一个cssQuery对象 javascript脚本实现代码
- js计算页面刷新的次数
- [原创]IE view-source 无法查看看源码 JavaScript看网页源码
- Prototype Hash对象 学习
- Prototype Array对象 学习