We may decide to execute a function not right now, but at a later time. That’s called “scheduling a call”.
There are two methods for it:
setTimeout
allows us to run a function once after the interval of time.setInterval
allows us to run a function repeatedly, starting after the interval of time, then repeating continuously at that interval.These methods are not a part of JavaScript specification. But most environments have an internal scheduler and provide these methods. In particular, they are supported in all browsers and Node.js.
The syntax:
let timerId = setTimeout(func|code, [delay], [arg1], [arg2], ...)
Parameters:
func|code
: Function or a string of code to execute.
Usually, that’s a function. For historical reasons, a string of code can be passed, but that’s not recommended.
delay
: The delay before running, in milliseconds (1000 ms = 1 second), by default 0.
arg1
, arg2
: Arguments for the function (not supported in IE9-)
#javascript #programming #scheduling