---
metadata:
  - name: generator
    content: Diplodoc Platform v5.52.0
alternate:
  - https://yandex.com/dev/games/doc/en/sdk/sdk-example.md
  - https://yandex.com/dev/games/doc/hi/sdk/sdk-example.md
  - https://yandex.com/dev/games/doc/ko/sdk/sdk-example.md
  - https://yandex.com/dev/games/doc/ru/sdk/sdk-example.md
  - https://yandex.com/dev/games/doc/tr/sdk/sdk-example.md
  - https://yandex.com/dev/games/doc/vi/sdk/sdk-example.md
  - https://yandex.com/dev/games/doc/zh/sdk/sdk-example.md
  - href: zh/sdk/sdk-example.md
    type: text/markdown
    title: Markdown version
  - href: ../llms.txt
    type: text/markdown
    title: llms.txt
---
> **Documentation Index:** Fetch the complete configuration index at https://yandex.com/dev/games/doc/zh/llms.txt

# 示例

<!-- source: zh/_includes/script-common.md -->
<!-- source: zh/_includes/script/index-js.md -->

<!-- endsource: zh/_includes/script/index-js.md -->

<!-- source: zh/_includes/script/requirements-js.md -->

<!-- endsource: zh/_includes/script/requirements-js.md -->

<!-- source: zh/_includes/script/image-modal-js.md -->

<!-- endsource: zh/_includes/script/image-modal-js.md -->
<!-- endsource: zh/_includes/script-common.md -->

以下示例展示如何使用同步或异步方法配置 Yandex Games SDK。

{% list tabs %}

* 同步方法

    示例细节：

    - 没有为第一次广告调用设置[回调函数](*key_callback)。
    - 为第二次调用和后续每次调用指定了每个可能的回调。
    - 为**显示广告**按钮分配了 `'click'` 事件句柄（在每次点击该按钮时调用广告）。

    ```html showLineNumbers
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
        <meta name="mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <title>SDK 同步配置的页面示例</title>
        
        
    </head>
    <body>
        <button id="button">显示广告</button>
    </body>
    </html>
    ```

* 异步方法

    示例细节：

    - 为第一次广告调用设置了 [onClose](*key_onClose) 回调函数。
    - 为第二次调用和后续每次调用指定了每个可能的[回调函数](*key_callback)。
    - 在广告块关闭后运行的代码将添加到 `onClose` 回调函数中。
    - SDK 或回调函数造成的所有错误都会传递给 [onError](*key_onError) 函数。
    - 为**显示广告**按钮分配了 `'click'` 事件句柄（在每次点击该按钮时调用广告）。

    ```html showLineNumbers
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
        <meta name="mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <title>SDK 异步配置的页面示例</title>
        
    </head>
    <body>
    <!-- Yandex Games SDK -->
    
    <button id="button">显示广告</button>
    </body>
    </html>
    ```

{% endlist %}

[*key_callback]: - `onClose`：在广告关闭、发生错误或广告因调用过于频繁而未能打开的时候调用。与 `wasShown` 参数（`boolean` 类型）一起使用，它的值表示是否显示广告。
- `onOpen`：在成功打开广告的时候调用。
- `onError`：在发生错误的时候调用。错误对象会传递给回调函数。

[*key_onClose]: `onClose`：在广告关闭、发生错误或广告因调用过于频繁而未能打开的时候调用。与 `wasShown` 参数（`boolean` 类型）一起使用，它的值表示是否显示广告。

[*key_onError]: `onError`：在发生错误的时候调用。错误对象会传递给回调函数。

[*key_onOpen]: `onOpen`：在成功打开广告的时候调用。