Science, asked by lollll7530, 11 months ago

Explain JavaScript setInterval and clearInterval function with example?

Answers

Answered by christeena1
0

hey

clearInterval()

The clearInterval() method clears a timer set with the setInterval() method.

The ID value returned by setInterval() is used as the parameter for the clearInterval() method.

Note: To be able to use the clearInterval() method, you must use a variable when creating the interval method:

myVar = setInterval("javascript function", milliseconds);

Then you will be able to stop the execution by calling the clearInterval() method.

Example

Display the current time (the setInterval() method will execute the "myTimer" function once every 1 second).

Use clearInterval() to stop the time:

var myVar = setInterval(myTimer, 1000);

function myTimer() {

   var d = new Date();

   var t = d.toLocaleTimeString();

   document.getElementById("demo").innerHTML = t;

}

function myStopFunction() {

   clearInterval(myVar);

}

setInterval()

The setInterval() method calls a function or evaluates an expression at specified intervals (in milliseconds).

The setInterval() method will continue calling the function until clearInterval() is called, or the window is closed.

The ID value returned by setInterval() is used as the parameter for the clearInterval() method.

Tip: 1000 ms = 1 second.

Tip: To execute a function only once, after a specified number of milliseconds, use the setTimeout() method.

Example

Alert "Hello" every 3 seconds (3000 milliseconds):

setInterval(function(){ alert("Hello"); }, 3000);


pl make me brainlist



Similar questions