Η μέθοδος Window clearTimeout()

Ορισμός και χρήση

clearTimeout() μέθοδο καθαρισμού The setTimeout() method το ρυθμισμένο χρονικό πρόγραμμα.

προειδοποίηση

Για να καθαρίσεις το timeout, χρησιμοποίησε τη μέθοδο από The setTimeout() method το αναγνωριστικό που επιστρέφεται:

myTimeout = setTimeout(function, milliseconds);

τότε μπορείς να καλέσεις clearTimeout() to stop execution:

clearTimeout(myTimeout);

See also:

The setTimeout() method

setInterval() method

clearInterval() method

Instance

Example 1

How to prevent myGreeting() from executing:

const myTimeout = setTimeout(myGreeting, 3000);
function myGreeting() {
  document.getElementById("demo").innerHTML = "Happy Birthday to You !!"
}
function myStopFunction() {
  clearTimeout(myTimeout);
}

Try it yourself

Example 2

This example has a 'Start' button to start the timer, an input field for the counter, and a 'Stop' button to stop the timer:

<button onclick="startCount()">Start count!</button>
<input type="text" id="demo">
<button onclick="stopCount()">Stop count!</button>
<script>
let counter = 0;
let timeout;
let timer_on = 0;
function timedCount() {
  document.getElementById("demo").value = counter;
  counter++;
  timeout = setTimeout(timedCount, 1000);
}
function startCount() {
  if (!timer_on) {
    timer_on = 1;
    timedCount();
  }
}
function stopCount() {
  clearTimeout(timeout);
  timer_on = 0;
}
</script>

Try it yourself

Syntax

clearTimeout(timeoutId)

Parameter

Parameter Description
timeoutId Required.The setTimeout() method The returned id.

Return value

None.

Description

clearTimeout() The method cancels the execution of specified code, called The setTimeout() method can delay the execution of these codes. Parameter timeoutId is called The setTimeout() method The returned value after, it identifies the delayed execution code block to be canceled (can have multiple).

Browser supports

所有浏览器都支持 clearTimeout():

Chrome IE Edge Firefox Safari Opera
Chrome IE Edge Firefox Safari Opera
Υποστήριξη Υποστήριξη Υποστήριξη Υποστήριξη Υποστήριξη Υποστήριξη