概要
window.setTimeout() によって設定された遅延を解除します。
構文
window.clearTimeout(timeoutID)
timeoutID は、解除したいタイマの ID です。ID は、window.setTimeout() の戻り値によって取得できます。
例
Web ページのコンテキストにおいて、以下のスクリプトを実行し、ページを一度クリックしてください。すぐに、メッセージがポップアップされます。絶え間なくページをクリックし続けると、その警告は決して現れません。
var alarm = {
remind: function(aMessage) {
alert(aMessage);
delete this.timeoutID;
},
setup: function() {
this.cancel();
var self = this;
this.timeoutID = window.setTimeout(function(msg) {self.remind(msg);}, 1000, "Wake up!");
},
cancel: function() {
if(typeof this.timeoutID == "number") {
window.clearTimeout(this.timeoutID);
delete this.timeoutID;
}
}
};
window.onclick = function() { alarm.setup() };
注記
clearTimeout へ妥当ではない ID を渡しても、何の効果もありません(また、例外はスローされません)。
仕様
DOM Level 0。どの標準にも属しません。