The React Native Timer uses the same methods as the standard JavaScript timer and is an implementation of that timer.
Code typically executes synchronously, or one line at a time, with each line only starting to run once before it has finished. However, when running code asynchronously or according to conditions rather than in a set, the order is frequently much better.
We can run sections of code asynchronously for predetermined amounts of time using the React Native Timer.
So, as mentioned, React Native implements the browser timers, a crucial and essential component of any application. Let’s look at its main examples.
setTimeout, clearTimeout
setTimeout can be used when there are business requirements to run a particular piece of code after a certain amount of time has passed or after a delay; clearTimeout is simply used to stop the timer that has already been started.
setTimeout(() => {
yourFunction();
}, 3000);
setInterval, clearInterval
The method setInterval uses the second parameter to specify exact time intervals at which to execute a function or other pieces of code.
setInterval(() => {
console.log('Interval triggered');
}, 1000);
An interval-bound function or block of code runs until it is halted. The clearInterval() method can be used to end an interval.
setImmediate, clearImmediate
Calling the function or execution as soon as possible.
var immediateID = setImmediate(function);
// The below code displays the alert dialog immediately.
var immediateId = setImmediate(
() => { alert('Immediate Alert');
}
To reverse the immediate actions that setImmediate had set, use clearImmediate().
requestAnimationFrame, cancelAnimationFrame
It is how animations are typically done. updating an animation before the subsequent animation frame by calling a function.
var requestID = requestAnimationFrame(function);
// The following code performs the animation.
var requestId = requestAnimationFrame(
() => { // animate something}
)
cancelAnimationFrame is used for canceling the function that was set by requestAnimationFrame().