JavaScript call function after delay

What you will learn here about JavaScript

  • JavaScript call function after delay
  • JavaScript call function every interval
  • JavaScript call function every 5 seconds

JavaScript call function after delay

Its very easy to call function after certain time delay or interval. JavaScript has setTimeout() function which call another function after certain delay or interval.

Syntax of setTimeout() method:

setTimeout(function_name, delay_in_miliseconds);

Example of setTimeout():

function test()
{
document.getElementById(“result”).innerHTML=”Test method is called”;
}
setTimeout(test, 5000); // this will call test function after 5 seconds

JavaScript call function every interval

In JavaScript we can call a particular function repeatedly after certain time delay. JavaScript has setInterval() method which calls another function repeatedly after certain duration.

Syntax of setInterval() method:

setInterval(function_name, delay_in_miliseconds);

Example of setInterval():

function test()
{
document.getElementById(“result”).innerHTML=”Test method is called”;
}
setInterval(test, 3000); // this will call test function every 3 seconds

JavaScript call function every 5 seconds

Example of JavaScript call function every 5 seconds is given below

function test()
{
document.getElementById(“result”).innerHTML=”Test method is called”;
}
setInterval(test, 5000); // this will call test function every 5 seconds

You may also like...