# Wait For Function

Source: https://docs.scrappey.com/docs/wait-for-function

> Waits until a custom JavaScript expression evaluates to `true` inside the browser, useful for dynamic content or specific page states.

## Command

`"wait_for_function"`

## Parameters

| Parameter | Type   | Required | Description                                                         |
| --------- | ------ | -------- | ------------------------------------------------------------------- |
| `code`    | string | Yes      | JavaScript code to evaluate. Must return a truthy value to proceed. |
| `timeout` | number | No       | Max time (ms) to wait for the condition (default: 60000 ms).        |

## Example

#### Wait for element to appear

```javascript
document.querySelector('.dynamic-content') !== null
```

#### Wait for text and status change

```javascript
const element = document.querySelector('.status');
return element &&
       element.textContent.includes('Ready') &&
       !element.classList.contains('loading')
```

#### Example Request (cURL)

```bash
curl -X POST "https://publisher.scrappey.com/api/v1?key=YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "cmd": "request.get",
    "url": "https://example.com",
    "browserActions": [
      {
        "type": "wait_for_function",
        "code": "document.querySelector('.dynamic-content') !== null",
        "timeout": 30000
      },
      {
        "type": "wait_for_function",
        "code": "const el = document.querySelector('.status'); return el && el.textContent.includes('Ready') && !el.classList.contains('loading');"
      }
    ]
  }'
```

## Notes

- The condition is evaluated in the **browser context**.
- It **polls every 200ms** until the condition is met.
- **Throws an error** if the timeout is reached.
- Any **JavaScript expression that evaluates to true/false** can be used.
