> ## Documentation Index
> Fetch the complete documentation index at: https://www.text.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Examples

> Example usage of the Chat Widget JavaScript API.

Here you can find some example usage of the Chat Widget JavaScript API. They all require the Widget to be installed on the page and `window.LiveChatWidget` to be defined.

## Show the Widget after time

Show the Chat Widget after 30 seconds and keep it open after reloading.

```js theme={null}
LiveChatWidget.on("visibility_changed", function (data) {
  if (data.visibility === "maximized") {
    localStorage.setItem("livechat_chat_visible", true);
  }
});

var isWidgetVisible = Boolean(localStorage.getItem("livechat_chat_visible"));

if (!isWidgetVisible) {
  setTimeout(function () {
    LiveChatWidget.call("maximize");
  }, 30000);
}
```

## Open the Widget using the button

Show the hidden or minimized Widget after a button has been clicked.
You can change the Widget visibility as described in our Help Center.

```js theme={null}
var chatButton = document.getElementById("chat-btn");
chatButton.addEventListener("click", function () {
  LiveChatWidget.call("maximize");
});
```

## Prefill username and email

It sets the Customers name and email using their login and email.

```js theme={null}
var user = JSON.parse(localStorage.getItem("user"));

LiveChatWidget.call("set_customer_name", user.login);
LiveChatWidget.call("set_customer_email", user.email);
```

## Initialize Widget asynchronously on button click

Initialize Chat Widget after clicking the button.

```html theme={null}
<html>
  <head>
    <script>
      window.__lc = window.__lc || {};
      window.__lc.organizationId = "<ORGANIZATION_ID>";
      window.__lc.asyncInit = true;

      /* rest of the standard snippet code */
    </script>
  </head>
  <body>
    <button id="chat-btn">Chat with us!</button>
    <script>
      var chatButton = document.getElementById("chat-btn");
      chatButton.addEventListener("click", function () {
        // Initialize Chat Widget
        LiveChatWidget.init();
      });
    </script>
  </body>
</html>
```
