ترتيب تحميل اللعبة يدويًا باستخدام onReady
تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
عندما يتم تحميل الصفحة التي تحتوي على لعبتك للمرة الأولى، تحدث عدة أحداث غير متزامنة.
يتم تحميل منطق اللعبة، ويتم تحميل علامة الإعلان، وتبدأ واجهة برمجة التطبيقات لموضع الإعلان في عملية التهيئة، ويمكن تحميل الإعلانات مسبقًا. نتيجةً لذلك، إذا طلبت adBreak()
بعد تحميل الصفحة بوقت قصير جدًا، من المحتمل ألا تكون واجهة برمجة التطبيقات قد تم تهيئتها أو أنّ الإعلانات لم تنتهِ من التحميل المُسبَق. إذا لم يتم تهيئة واجهة برمجة التطبيقات، سيتعذّر إجراء المكالمة، وإذا كنت قد سجّلت adBreakDone()
دالة ردّ الاتصال، سيتم ضبط breakStatus
على notReady
.
إذا كنت بحاجة إلى مزامنة منطق لعبتك مع عملية تهيئة واجهة برمجة التطبيقات AdPlacement API، يمكنك استخدام معاودة الاتصال onReady()
إلى adConfig()
.
يتم استدعاء onReady()
من خلال "واجهة برمجة التطبيقات لموضع الإعلان" في الحالات التالية:
- تم تحميل علامة الإعلان
- تم تحميل واجهة برمجة التطبيقات لموضع الإعلان وإعدادها، و
- انتهت عملية التحميل المُسبق للإعلانات، إذا طلبت التحميل المُسبق باستخدام
adConfig()
في هذه المرحلة، يتم إعداد واجهة برمجة التطبيقات لموضع الإعلان بالكامل. يمكنك طلب 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()
. مزيد من المعلومات عن الإعلانات قبل التشغيل
عملية استبعاد للقناة لمهلة معيّنة
لا يمكن ضمان استدعاء onReady()
إذا تأخّر تهيئة Ad Placement API أو تعذّر تحميله بالكامل. لضمان بدء لعبتك في الوقت المناسب، يمكنك إعداد مهلة زمنية. إذا تلقّيت
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 ما لم يُنصّ على خلاف ذلك، ونماذج الرموز مرخّصة بموجب ترخيص Apache 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"]]