国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

Prototype PeriodicalExecuter對象 學習

2024-05-06 14:14:40
字體:
來源:轉載
供稿:網友
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...
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 巴东县| 岳阳县| 大丰市| 涞水县| 鄂托克前旗| 孟村| 宝鸡市| 镇赉县| 林芝县| 兴安盟| 龙南县| 南溪县| 河东区| 赣榆县| 南雄市| 邵阳市| 伊金霍洛旗| 泗水县| 牡丹江市| 奉化市| 抚顺县| 玉环县| 锦州市| 金门县| 闻喜县| 淮北市| 安庆市| 芦溪县| 合肥市| 溆浦县| 普格县| 鲁山县| 琼海市| 赣榆县| 化隆| 武胜县| 鸡东县| 丹巴县| 临猗县| 桦甸市| 邹城市|