timers
Stability: 2 - Stable
The timers module exposes global APIs for scheduling functions to run at some point in the future. Since timer functions are global, you can use them directly without referencing timers.
Timer functions in Auto.js are similar to the ones in web browsers, but they are implemented differently under the hood. Auto.js builds timers on Android’s Looper/Handler message loop, and the behavior is closer to Node.js than a typical browser environment.
For example, to show a message "hello" after 5 seconds:
setTimeout(function () {
toast("hello");
}, 5000);Note that timers are still single-threaded. If your main script is blocked by a long-running operation or an infinite loop, scheduled timers may not run on time. For example:
setTimeout(function () {
// This runs after ~15 seconds, not 5 seconds
toast("hello");
}, 5000);
// Sleep for 10 seconds
sleep(10000);Or:
setTimeout(function () {
// This will never run
toast("hello");
}, 5000);
// Infinite loop
while (true);setInterval(callback, delay[, ...args])
callback{Function} Function to call when the timer fires.delay{number} Milliseconds to wait before each call....args{any} Optional arguments passed tocallback.
Schedules callback to run repeatedly every delay milliseconds. Returns an id that can be passed to clearInterval().
If delay is less than 0, it will be treated as 0.
setTimeout(callback, delay[, ...args])
callback{Function} Function to call when the timer fires.delay{number} Milliseconds to wait before callingcallback....args{any} Optional arguments passed tocallback.
Schedules a one-time callback to run after delay milliseconds. Returns an id that can be passed to clearTimeout().
callback may not run at the exact delay time. Auto.js cannot guarantee the precise trigger time or ordering of callbacks; it will run them as close to the specified time as possible.
If delay is less than 0, it will be treated as 0.
setImmediate(callback[, ...args])
callback{Function} Function to call at the end of the current Looper tick....args{any} Optional arguments passed tocallback.
Schedules callback to run immediately after I/O callbacks. Returns an id that can be passed to clearImmediate().
When setImmediate() is called multiple times, callbacks run in the order they were created. Each event-loop iteration processes the whole callback queue. If an immediate is queued by a currently running callback, it will not fire until the next event-loop iteration.
setImmediate(), setInterval(), and setTimeout() each return a timer id. You can use these ids to cancel timers and prevent them from firing.
clearInterval(id)
id{number} An id returned bysetInterval().
Cancels a repeating timer created by setInterval().
Example:
// Send "hello" once every 5 seconds
var id = setInterval(function () {
toast("hello");
}, 5000);
// Cancel after 1 minute
setTimeout(function () {
clearInterval(id);
}, 60 * 1000);clearTimeout(id)
id{number} An id returned bysetTimeout().
Cancels a one-time timer created by setTimeout().
clearImmediate(id)
id{number} An id returned bysetImmediate().
Cancels an immediate created by setImmediate().
