How to call after some delay write program in JavaScript?

You can use the setTimeout function in JavaScript to call a function after a specified amount of time has elapsed. The setTimeout function takes two arguments: a callback function and the time delay in milliseconds. Here’s an example:

function myFunction() {
  console.log("Hello, I was called after a delay!");
}

setTimeout(myFunction, 5000);

In this example, myFunction will be called after 5 seconds (5000 milliseconds) have elapsed.

It’s also worth noting that setTimeout is a non-blocking function, meaning that the code will continue to run after the setTimeout call and won’t wait for the delay to complete before moving on. This can be useful for performing certain tasks in the background while the rest of your code continues to execute.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *