clearTimeout()
метод WindowOrWorkerGlobalScope
отменяет таймаут, ранее установленный вызовом setTimeout()
.
Синтаксис
scope.clearTimeout(timeoutID)
Параметры
timeoutID
- Идентификатор таймаута, который вы хотите отменить. Этот идентификатор был возвращен соответствующим вызовом
setTimeout()
.
It's worth noting that the pool of IDs used by setTimeout()
and setInterval()
are shared, which means you can technically use clearTimeout()
and clearInterval()
interchangeably. However, for clarity, you should avoid doing so.
Пример использования:
Запустите приведенный ниже скрипт в контекте веб-страницы и кликните один раз. Вы увидите всплывающее сообщение через 1 секунду. Если вы щелкните страницу несколько раз за одну секунду, предупреждение появится только один раз.
var alarm = {
remind: function(aMessage) {
alert(aMessage);
this.timeoutID = undefined;
},
setup: function() {
if (typeof this.timeoutID === 'number') {
this.cancel();
}
this.timeoutID = window.setTimeout(function(msg) {
this.remind(msg);
}.bind(this), 1000, 'Wake up!');
},
cancel: function() {
window.clearTimeout(this.timeoutID);
}
};
window.onclick = function() { alarm.setup(); };
Примечания
Передача недействительного ID clearTimeout()
ни к чему не приведет. Исключение не создается.
Спецификация
Specification | Status | Comment |
---|---|---|
HTML Living Standard Определение 'WindowOrWorkerGlobalScope.clearTimeout()' в этой спецификации. |
Живой стандарт | Method moved to the WindowOrWorkerGlobalScope mixin in the latest spec. |
HTML Living Standard Определение 'clearTimeout()' в этой спецификации. |
Живой стандарт |
Совместимость с браузером
BCD tables only load in the browser
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.