delay()
Pauses script execution for a specified number of milliseconds. Essential for timing-dependent automations where you need to wait for UI elements, animations, or network requests.number
required
Duration to wait in milliseconds. Accepts any numeric type. Defaults to 1000ms (1 second) if invalid.
void
This function blocks execution and does not return a value.
Basic Usage
Practical Examples
Implementation Details
Source Location
MainActivity.kt:5462
Type Handling
The function safely converts input to aLong value:
- JavaScript numbers → Kotlin
Long - Invalid/null values → Default 1000ms
- Negative values → Treated as-is (no delay)
The delay blocks the current thread using
Thread.sleep(). During this time, no other script operations execute.Common Timing Patterns
Standard Delays
Retry Logic
Progressive Delays
Best Practices
Use constants for common delays
Use constants for common delays
Define timing constants at the top of your script for easy tuning.
Add delays after every UI interaction
Add delays after every UI interaction
Always add a delay after clicks, typing, or swipes to let the UI respond.
Adjust delays based on device performance
Adjust delays based on device performance
Slower devices may need longer delays. Consider device-specific timing.
Balance speed and reliability
Balance speed and reliability
Shorter delays = faster automation, but may cause failures. Find the sweet spot.
Common Use Cases
Wait for Page Load
Sequential Clicks
Form Entry Timing
Limitations
Alternatives
For more sophisticated timing:- Polling loops: Check for conditions repeatedly
- Scheduled tasks: Use schedule() for recurring actions
- Event-based: React to UI state changes rather than fixed delays
Related Functions
- schedule() - Schedule delayed or recurring tasks
- speakText() - Combine with delays for timed announcements
- magicClicker() - Vision-based UI automation