--> setTimeout(callbackFunction[, interval])
This can be understand as you are going to call a function with the specified name(Given by you) after an specified time.
--> You cannot do like this setTimeOut(1000);
because 1000 will be treated as callback function As the first parameter of setTimeout()
is callback function.
You cannot define a function like
function 1000() {
// Code Resides here.
}
because java script does not allow these type of function names.
--> You can pass anonymous function to setTimeout()
like
setTimeout(function() {
// Code resides here.
}[, interval]);
The above function code will be executed after the given interval. If no interval is given then it will executed immediately.
Note:- Parameters given in square brackets ([, ]) are optional to pass.
--> You can refer the below mentioned thread for your use: JavaScript sleep/wait before continuing
As this thread contains a function definition which works like sleep()
in other languages.