JS Coding Questions Logo
JS Coding Questions
#118💼 Interview💻 Code

What is the purpose of clearTimeout method

Advertisement

728x90

The clearTimeout() function is used in javascript to clear the timeout which has been set by setTimeout()function before that. i.e, The return value of setTimeout() function is stored in a variable and it’s passed into the clearTimeout() function to clear the timer.

For example, the below setTimeout method is used to display the message after 3 seconds. This timeout can be cleared by the clearTimeout() method.

javascript
1<script>
2  var msg;
3  function greeting() {
4  alert('Good morning');
5  }
6  function start() {
7  msg =setTimeout(greeting, 3000);
8
9  }
10
11  function stop() {
12  clearTimeout(msg);
13  }
14  </script>

Advertisement

Responsive Ad
🎯 Practice NowRelated Challenge

JavaScript Coding Exercise 34

Test your knowledge with this interactive coding challenge.

Start Coding

Advertisement

728x90
118of476