0 votes
in JavaScript by
What is the purpose of clearInterval method?

1 Answer

0 votes
by

The clearInterval() function is used in javascript to clear the interval which has been set by setInterval() function. i.e, The return value returned by setInterval() function is stored in a variable and it’s passed into the clearInterval() function to clear the interval.

For example, the below setInterval method is used to display the message for every 3 seconds. This interval can be cleared by the clearInterval() method.

<script>
var msg;
function greeting() {
   alert('Good morning');
}
function start() {
  msg = setInterval(greeting, 3000);

}

function stop() {
    clearInterval(msg);
}
</script>

Related questions

0 votes
asked Oct 6, 2023 in JavaScript by GeorgeBell
0 votes
asked Oct 24, 2023 in JavaScript by DavidAnderson
...