最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Prototype PeriodicalExecuter對象 學習

 更新時間:2009年07月19日 01:25:04   作者:  
這個對象就是可以周期性的執(zhí)行某個方法,但是在它內部維持了一個狀態(tài),可以防止由于某些原因一次調用沒執(zhí)行,然后下一次調用又來了,這樣會造成連續(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í)行某個方法,但是在它內部維持了一個狀態(tài),可以防止由于某些原因一次調用沒執(zhí)行,然后下一次調用又來了,這樣會造成連續(xù)執(zhí)行兩次方法。上面的第二斷英文就是這個意思。

幫助文檔上說這個對象只提供了一個方法stop,但是在我看的源碼里還提供了一個事件onTimerEvent,應該可以在某個時候觸發(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...

相關文章

最新評論

永康市| 怀远县| 和林格尔县| 龙南县| 崇信县| 遂川县| 梨树县| 治县。| 高尔夫| 获嘉县| 修文县| 霸州市| 西峡县| 西和县| 大安市| 全椒县| 大港区| 盈江县| 洛浦县| 科技| 吴江市| 商丘市| 马公市| 北票市| 昭通市| 林芝县| 新邵县| 巴东县| 资溪县| 德清县| 通辽市| 图片| 桃江县| 灵武市| 万山特区| 邛崃市| 平顶山市| 洪泽县| 张家界市| 贵阳市| 福海县|