Prototype PeriodicalExecuter對象 學(xué)習(xí)
更新時間:2009年07月19日 01:25:04 作者:
這個對象就是可以周期性的執(zhí)行某個方法,但是在它內(nèi)部維持了一個狀態(tài),可以防止由于某些原因一次調(diào)用沒執(zhí)行,然后下一次調(diào)用又來了,這樣會造成連續(xù)執(zhí)行兩次方法。上面的第二斷英文就是這個意思。
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.
這個對象就是可以周期性的執(zhí)行某個方法,但是在它內(nèi)部維持了一個狀態(tài),可以防止由于某些原因一次調(diào)用沒執(zhí)行,然后下一次調(diào)用又來了,這樣會造成連續(xù)執(zhí)行兩次方法。上面的第二斷英文就是這個意思。
幫助文檔上說這個對象只提供了一個方法stop,但是在我看的源碼里還提供了一個事件onTimerEvent,應(yīng)該可以在某個時候觸發(fā)這個事件。但幫助文檔上沒有給出示例。
這個對象源碼比較簡單,這里直接貼出來了,就不再注釋了:
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.
這個對象就是可以周期性的執(zhí)行某個方法,但是在它內(nèi)部維持了一個狀態(tài),可以防止由于某些原因一次調(diào)用沒執(zhí)行,然后下一次調(diào)用又來了,這樣會造成連續(xù)執(zhí)行兩次方法。上面的第二斷英文就是這個意思。
幫助文檔上說這個對象只提供了一個方法stop,但是在我看的源碼里還提供了一個事件onTimerEvent,應(yīng)該可以在某個時候觸發(fā)這個事件。但幫助文檔上沒有給出示例。
這個對象源碼比較簡單,這里直接貼出來了,就不再注釋了:
復(fù)制代碼 代碼如下:
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;
}
}
}
});
看一下示例:
復(fù)制代碼 代碼如下:
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...
相關(guān)文章
Prototype的Class.create函數(shù)解析
Prototype中的類的創(chuàng)建,一般使用Class.create方法來創(chuàng)建,例如PeriodicalExecuter類型。使用的時候通過調(diào)用new PeriodicalExecuter(xxx)來生成對象。2011-09-09[轉(zhuǎn)]prototype 源碼解讀 超強(qiáng)推薦
[轉(zhuǎn)]prototype 源碼解讀 超強(qiáng)推薦...2007-02-02初學(xué)prototype,發(fā)個JS接受URL參數(shù)的代碼
初學(xué)prototype,發(fā)個JS接受URL參數(shù)的代碼...2007-01-01Prototype Number對象 學(xué)習(xí)
這個對象提供一些操作數(shù)值類型的工具函數(shù)2009-07-07Prototype Enumerable對象 學(xué)習(xí)
Enumerable是Prototype框架的基石,而Enumerable不單獨使用,在Prototype中其它對象mix了Enumerable里面的方法,這樣就可以在這些對象上應(yīng)用Enumerable的方法,這樣的對象有:Array,Hash,ObjectRange,還有一些和DOM,AJAX相關(guān)的對象。2009-07-07使用prototype.js 的時候應(yīng)該特別注意的幾個問題.
使用prototype.js 的時候應(yīng)該特別注意的幾個問題....2007-04-04