توالی بارگذاری بازی را با onReady به صورت دستی تنظیم کنید
با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
هنگامی که صفحه حاوی بازی شما برای اولین بار بارگیری می شود، تعدادی رویداد ناهمزمان رخ می دهد.
منطق بازی شما بارگیری میشود، برچسب تبلیغات بارگیری میشود، API قرار دادن آگهی شروع به تنظیم اولیه میکند، و تبلیغات را میتوان از قبل بارگیری کرد. در نتیجه، اگر خیلی زود پس از بارگذاری صفحه، adBreak()
فراخوانی کنید، این احتمال وجود دارد که API مقداردهی اولیه نشده باشد یا تبلیغات از قبل بارگیری نشده باشد. اگر API مقداردهی اولیه نشده باشد، تماس با شکست مواجه میشود، و اگر یک تماس adBreakDone()
را ثبت کرده باشید، breakStatus
روی notReady
تنظیم میشود.
اگر نیاز دارید که منطق بازی خود را با مقداردهی اولیه Ad Placement API همگام کنید، می توانید از callback onReady()
به adConfig()
استفاده کنید.
onReady()
توسط Ad Placement API فراخوانی می شود که:
- برچسب آگهی بارگیری شده است،
- 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
به طور خودکار تمام منطق پیش بارگذاری و زمان بندی مورد نیاز را پیاده سازی می کند. اگر از preroll با بازی خود استفاده می کنید، نیازی به استفاده onReady()
ندارید. درباره preroll بیشتر بدانید
تایم اوت ها
اگر راهاندازی API Ad Placement به تأخیر بیفتد یا به طور کامل بارگیری نشود، تضمین نمیشود که 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>
جز در مواردی که غیر از این ذکر شده باشد،محتوای این صفحه تحت مجوز Creative Commons Attribution 4.0 License است. نمونه کدها نیز دارای مجوز Apache 2.0 License است. برای اطلاع از جزئیات، به خطمشیهای سایت Google Developers مراجعه کنید. جاوا علامت تجاری ثبتشده Oracle و/یا شرکتهای وابسته به آن است.
تاریخ آخرین بهروزرسانی 2025-07-24 بهوقت ساعت هماهنگ جهانی.
[null,null,["تاریخ آخرین بهروزرسانی 2025-07-24 بهوقت ساعت هماهنگ جهانی."],[[["\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"]]