Saturday, March 30, 2024

What are setTimeOut and setTimeInterval in Javascript?

JavaScript can be executed in time-intervals. This is called timing events.

The two key methods to use with JavaScript are:

a) setTimeout(function, milliseconds)

Executes a function, after waiting a specified number of milliseconds.

As an example, in the below example myFunction will be called after 1 second.

let myvar=setTimeout(myFunction, 1000);

function myFunction() {

  console.log('myFunction called');

}

b) setInterval(function, milliseconds)

Same as setTimeout(), but repeats the execution of the function continuously.

let myvar=setInterval(myFunction, 1000);

function myFunction() {

  console.log('myFunction called');

}

No comments:

Post a Comment