javascript-timers Flashcards
What is a “callback” function?
A callback function is:
a function passed into another function as an argument,
which is then invoked inside the outer function to
complete some kind of routine or action.
Besides adding an event listener callback function to
an element or the document,
what is one way to delay the execution of
a JavaScript function until some point in the future?
setTimeout(functionRef, delay)
setTimeout(code)
setTimeout(code, delay)
setTimeout(functionRef)
setTimeout(functionRef, delay)
setTimeout(functionRef, delay, param1)
setTimeout(functionRef, delay, param1, param2)
setTimeout(functionRef, delay, param1, param2, /* … ,*/ paramN)
Parameters
- functionRef : A function to be executed after the timer expires.
- code : An alt syntax that
allows you to include a string instead of a function,
which is compiled and executed when the timer exp. (not recommended) - delay (Optional) : The time, in milliseconds that the timer
should wait BEFORE the specified function or code is executed.If this parameter is omitted, a value of 0 is used, meaning execute "immediately", or more accurately, the next event cycle.
How can you set up a function to be called
repeatedly without using a loop?
To call a function repeatedly (e.g., every N milliseconds),
consider using setInterval().
The setInterval() method, offered on the Window and Worker interfaces, repeatedly calls a function or executes a code snippet,
with a fixed time delay between each call.
This method returns an interval ID
which uniquely identifies the interval,
so you can remove it later by calling clearInterval().
What is the default time delay
if you omit the delay parameter from setTimeout() or setInterval()?
Both setTimeout and setInterval
Defaults to 0 if not specified.
What do setTimeout() and setInterval() return?
The global setTimeout() method : sets a timer
which executes a function or specified piece of code
once the timer expires.
The returned timeoutID is a positive integer value
which identifies the timer created by the call to setTimeout().
This value can be passed to clearTimeout() to cancel the timeout.
Its GUARENTEED that a timeoutID value will never be reused by
a subsequent call to setTimeout() or setInterval() on the same object
(a window or a worker).
If setTimeout() is called with delay value that’s not a number,
implicit type coercion is silently done on
the value to convert it to a number.
What does setInterval() return?
Return value: The returned intervalID is a numeric, non-zero value
which identifies the timer created by the call to setInterval();
this value can be passed to clearInterval() to cancel the interval.