How to delay a JavaScript function call using JavaScript?

JavascriptWeb DevelopmentFront End Technology

To delay a function call, use setTimeout() function.

setTimeout(functionname, milliseconds, arg1, arg2, arg3...)

The following are the parameters −

functionname − The function name for the function to be executed.

milliseconds − The number of milliseconds.

arg1, arg2, arg3 − These are the arguments passed to the function.


You can try to run the following code to delay a JavaScript function call with a setTimeout() callback.

 Live Demo

<!DOCTYPE html>
      <button onclick="timeFunction()">Submit</button>
         function timeFunction() {
            setTimeout(function(){ alert("After 5 seconds!"); }, 5000);
<p>Click the above button and wait for 5 seconds.</p>
Updated on 12-Jun-2020 11:45:42