Prototype PeriodicalExecuter對(duì)象 學(xué)習(xí)
更新時(shí)間:2009年07月19日 01:25:04 作者:
這個(gè)對(duì)象就是可以周期性的執(zhí)行某個(gè)方法,但是在它內(nèi)部維持了一個(gè)狀態(tài),可以防止由于某些原因一次調(diào)用沒執(zhí)行,然后下一次調(diào)用又來了,這樣會(huì)造成連續(xù)執(zhí)行兩次方法。上面的第二斷英文就是這個(gè)意思。
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.
這個(gè)對(duì)象就是可以周期性的執(zhí)行某個(gè)方法,但是在它內(nèi)部維持了一個(gè)狀態(tài),可以防止由于某些原因一次調(diào)用沒執(zhí)行,然后下一次調(diào)用又來了,這樣會(huì)造成連續(xù)執(zhí)行兩次方法。上面的第二斷英文就是這個(gè)意思。
幫助文檔上說這個(gè)對(duì)象只提供了一個(gè)方法stop,但是在我看的源碼里還提供了一個(gè)事件onTimerEvent,應(yīng)該可以在某個(gè)時(shí)候觸發(fā)這個(gè)事件。但幫助文檔上沒有給出示例。
這個(gè)對(duì)象源碼比較簡單,這里直接貼出來了,就不再注釋了:
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.
這個(gè)對(duì)象就是可以周期性的執(zhí)行某個(gè)方法,但是在它內(nèi)部維持了一個(gè)狀態(tài),可以防止由于某些原因一次調(diào)用沒執(zhí)行,然后下一次調(diào)用又來了,這樣會(huì)造成連續(xù)執(zhí)行兩次方法。上面的第二斷英文就是這個(gè)意思。
幫助文檔上說這個(gè)對(duì)象只提供了一個(gè)方法stop,但是在我看的源碼里還提供了一個(gè)事件onTimerEvent,應(yīng)該可以在某個(gè)時(shí)候觸發(fā)這個(gè)事件。但幫助文檔上沒有給出示例。
這個(gè)對(duì)象源碼比較簡單,這里直接貼出來了,就不再注釋了:
復(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)文章
基礎(chǔ)的prototype.js常用函數(shù)及其用法
基礎(chǔ)的prototype.js常用函數(shù)及其用法...2007-03-03
prototype 1.5相關(guān)知識(shí)及他人筆記
prototype 1.5相關(guān)知識(shí)及他人筆記...2006-12-12
Prototype RegExp對(duì)象 學(xué)習(xí)
幫助文檔上沒有這個(gè)對(duì)象,實(shí)際上源代碼中這個(gè)對(duì)象還是有方法的,就1靜態(tài)方法,作用也不是很大,這里簡單說一下,因?yàn)橐院蠼榻B別的對(duì)象時(shí)會(huì)用到這個(gè)RegExp2009-07-07
[轉(zhuǎn)]prototype 源碼解讀 超強(qiáng)推薦
[轉(zhuǎn)]prototype 源碼解讀 超強(qiáng)推薦...2007-02-02
prototype Element學(xué)習(xí)筆記(篇二)
這一篇主要是要總論Element的所有函數(shù)。2008-10-10
Prototype Template對(duì)象 學(xué)習(xí)
這里的Template對(duì)象其實(shí)就是格式化字符串的工具,就像java中的String.format方法。這個(gè)對(duì)象只提供一個(gè)方法evaluate。2009-07-07
Prototype源碼淺析 String部分(四)之補(bǔ)充
Prototype源碼淺析 String部分(四)之補(bǔ)充,需要的朋友可以參考下。2012-01-01

