Глобальная функция clearTimeout()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

Глобальный метод clearTimeout() отменяет таймаут, ранее установленный вызовом 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 секунду. Если вы щёлкните страницу несколько раз за одну секунду, предупреждение появится только один раз.

js
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
HTML
# dom-cleartimeout-dev

Совместимость с браузерами

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
clearTimeout
Available in workers

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Partial support
Partial support

Смотрите также