使用 onReady 手动排定游戏加载顺序
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
当包含游戏的网页首次加载时,会发生许多异步事件。
游戏逻辑加载,广告代码加载,Ad Placement API 开始初始化,然后会预加载广告。因此,如果您在网页加载后不久即调用 adBreak()
,那么 API 可能尚未完成初始化或广告尚未预加载完毕。如果 API 尚未初始化,调用将失败,并且如果您已注册 adBreakDone()
回调,则 breakStatus
将设为 notReady
。
如果您需要将游戏逻辑与 Ad Placement API 初始化同步,则可以使用针对 adConfig()
的 onReady()
回调。
在以下情况下,Ad Placement API 会调用 onReady()
:
- 广告代码加载完毕,
- Ad Placement API 加载完毕并已初始化,并且
- 广告已预加载完毕(如果您使用
adConfig()
请求预加载)
此时,Ad Placement API 已完全初始化。您可以调用 adBreak()
,它不会返回 notReady
状态。但与往常一样,adBreak()
可能仍不会展示广告(例如,没有可投放的广告)。
以下示例展示了 onReady()
的用法:
...
<script>
window.adsbygoogle = window.adsbygoogle || [];
var adBreak = adConfig = function(o) {adsbygoogle.push(o);}
...
function init() {
// Game start logic, show loading screen
adConfig({
preloadAdBreaks: 'on',
onReady: showAd
});
// Don't start the gameplay just yet, keep loading.
}
function showAd() {
// Show an ad
adBreak({
type: 'start',
adBreakDone: startGame, // always called, unblocks the game logic
...
});
}
...
</script>
注意:最常见的使用情形是在网页加载后不久即调用 adBreak()
,以植入前贴片广告。我们强烈建议您使用 preroll
展示位置类型,而不要使用本文所述的方法来创建自己的展示位置。
preroll
会自动实施所有必需的预加载和超时逻辑。如果您在游戏中使用了前贴片广告,则无需使用 onReady()
。详细了解前贴片广告
超时
如果 Ad Placement API 初始化延迟或无法完全加载,则不能保证会调用 onReady()
。为确保游戏能及时启动,您可能需要设置超时。如果您收到 onReady()
回调,则可以调用 adBreak() 来投放广告,否则您可以跳过广告调用并继续加载游戏。
以下示例包含超时,它类似于前贴片广告展示位置实施的逻辑:
...
<script>
window.adsbygoogle = window.adsbygoogle || [];
var adBreak = adConfig = function(o) {adsbygoogle.push(o);}
...
function init() {
// Game start logic
let adConfigPromise =
new Promise((resolve, reject) => adConfig({
preloadAdBreaks: 'on',
onReady: () => resolve(true)
}));
let timeoutPromise =
new Promise((resolve, reject) => {
setTimeout(() => {
resolve(false);
}, 2000);
});
// Whatever happens first resolves this promise.
Promise.race([
adConfigPromise,
timeoutPromise
]).then((shouldShowPreRoll) => {
if (shouldShowPreRoll) {
showPreRoll();
} else {
startGame();
}
});
}
function showPreRoll() {
// Show ad
adBreak({
type: 'start',
adBreakDone: startGame, // always called, unblocks the game logic
...
});
}
...
</script>
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-26。
[null,null,["最后更新时间 (UTC):2025-07-26。"],[[["\u003cp\u003eThe Ad Placement API may not be immediately ready after page load, leading to potential ad break failures if called too soon.\u003c/p\u003e\n"],["\u003cp\u003eUse the \u003ccode\u003eonReady()\u003c/code\u003e callback with \u003ccode\u003eadConfig()\u003c/code\u003e to synchronize your game logic with the Ad Placement API initialization, ensuring ads are preloaded and ready.\u003c/p\u003e\n"],["\u003cp\u003eFor preroll ads, utilize the dedicated \u003ccode\u003epreroll\u003c/code\u003e placement type instead of manually managing preloading and timeouts.\u003c/p\u003e\n"],["\u003cp\u003eImplement a timeout mechanism with \u003ccode\u003eonReady()\u003c/code\u003e to avoid indefinite delays and ensure your game starts promptly, even if ad initialization fails.\u003c/p\u003e\n"]]],["The Ad Placement API loads asynchronously with game logic and ads. Calling `adBreak()` immediately might fail if the API hasn't initialized or ads aren't preloaded. Use `onReady()` in `adConfig()` to ensure the API is ready, including tag loading, initialization, and ad preloading. `onReady()` is not guaranteed to be called if the API fails to load entirely. To handle potential delays, implement a timeout using `Promise.race`, proceeding with or without an ad call based on the timeout.\n"],null,["# Manually sequence game loading with onReady\n\nWhen the page containing your game first loads there are a number of\nasynchronous events taking place.\n\nYour game logic loads, the ads tag loads, the Ad Placement API starts\ninitialising, and ads can be preloaded. As a result, if you call `adBreak()`\nvery soon after page load, there is a chance that the API may not have\ninitialized or that ads have not finished preloading. If the API has not\ninitialized the call will fail, and if you've registered an `adBreakDone()`\ncallback, the `breakStatus` will be set to `notReady`.\n\nIf you need to synchronize your game logic with the initialization of the Ad\nPlacement API, you can use the `onReady()` callback to `adConfig()`.\n\n`onReady()` is called by the Ad Placement API when:\n\n- the ad's tag has loaded,\n- the Ad Placement API has loaded and initialized, and\n- ads have finished preloading---if you requested preloading using `adConfig()`\n\nAt this point the Ad Placement API is fully initialized. You can call\n`adBreak()`, and it won't return a `notReady` status. But as always `adBreak()`\nmay still not show an ad (for example, there wasn't an ad available).\n\nHere's an example that shows the use of `onReady()`: \n\n ...\n \u003cscript\u003e\n window.adsbygoogle = window.adsbygoogle || [];\n var adBreak = adConfig = function(o) {adsbygoogle.push(o);}\n ...\n function init() {\n // Game start logic, show loading screen\n adConfig({\n preloadAdBreaks: 'on',\n onReady: showAd\n });\n // Don't start the gameplay just yet, keep loading.\n }\n\n function showAd() {\n // Show an ad\n adBreak({\n type: 'start',\n adBreakDone: startGame, // always called, unblocks the game logic\n ...\n });\n }\n ...\n \u003c/script\u003e\n\n**Note** : the most common use case to call `adBreak()` very soon after page load is to implement a preroll. We **strongly recommend** you use a `preroll` placement type rather than trying to create your own using the methods described here.\n\n`preroll` automatically implements all of the required preloading and timeout logic. If you use a preroll with your game, you don't need to use `onReady()`. Learn more about [preroll](/ad-placement/docs/preload-ads)\n\nTimeouts\n--------\n\n`onReady()` is not guaranteed to be called if the Ad Placement API\ninitialisation is delayed or fails to load entirely. To ensure your game starts\nin a timely fashion, you may want to set up a timeout. If you receive the\n`onReady()`callback, then you can call adBreak() to place the ad, otherwise you\ncan skip the ad call and proceed to load your game.\n\nThe following example includes a timeout---and is similar to the logic\nimplemented by a preroll placement: \n\n ...\n \u003cscript\u003e\n window.adsbygoogle = window.adsbygoogle || [];\n var adBreak = adConfig = function(o) {adsbygoogle.push(o);}\n ...\n function init() {\n // Game start logic\n let adConfigPromise =\n new Promise((resolve, reject) =\u003e adConfig({\n preloadAdBreaks: 'on',\n onReady: () =\u003e resolve(true)\n }));\n let timeoutPromise =\n new Promise((resolve, reject) =\u003e {\n setTimeout(() =\u003e {\n resolve(false);\n }, 2000);\n });\n // Whatever happens first resolves this promise.\n Promise.race([\n adConfigPromise,\n timeoutPromise\n ]).then((shouldShowPreRoll) =\u003e {\n if (shouldShowPreRoll) {\n showPreRoll();\n } else {\n startGame();\n }\n });\n }\n\n function showPreRoll() {\n // Show ad\n adBreak({\n type: 'start',\n adBreakDone: startGame, // always called, unblocks the game logic\n ...\n });\n }\n ...\n \u003c/script\u003e"]]