Skip to main content

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 a Long 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

Define timing constants at the top of your script for easy tuning.
Always add a delay after clicks, typing, or swipes to let the UI respond.
Slower devices may need longer delays. Consider device-specific timing.
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

  • Blocks script execution (synchronous)
  • Cannot be interrupted once started
  • Does not account for dynamic load times
  • Very long delays (>30s) may trigger watchdogs

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