使用 onReady 手動排序遊戲載入順序
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
當含有遊戲的網頁首次載入時,會發生多個非同步事件。
遊戲邏輯會載入、廣告代碼會載入、Ad Placement API 會開始初始化,且廣告可以預先載入。因此,如果您在網頁載入後不久就呼叫 adBreak()
,API 可能尚未初始化,或是廣告尚未完成預先載入。如果 API 尚未初始化,呼叫就會失敗。如果您已註冊 adBreakDone()
回呼,breakStatus
會設為 notReady
。
如要將遊戲邏輯與 Ad Placement API 的初始化作業同步處理,可以使用 onReady()
回呼來 adConfig()
。
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>
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-26 (世界標準時間)。
[null,null,["上次更新時間: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"]]