示例
以下示例展示如何使用同步或异步方法配置 Yandex Games SDK。
同步方法
异步方法
示例细节:
- 没有为第一次广告调用设置回调函数。
- 为第二次调用和后续每次调用指定了每个可能的回调。
- 为显示广告按钮分配了
'click'事件句柄(在每次点击该按钮时调用广告)。
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <meta charset="UTF-8">
5 <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
6 <meta name="mobile-web-app-capable" content="yes">
7 <meta name="apple-mobile-web-app-capable" content="yes">
8 <title>SDK 同步配置的页面示例</title>
9 <script src="/sdk.js"></script>
10 <script>
11 YaGames.init().then(ysdk => {
12 ysdk.adv.showFullscreenAdv();
13
14 const buttonElem = document.querySelector('#button');
15
16 let commonCounter = 0;
17 buttonElem.addEventListener('click', () => {
18 let counter = 0;
19
20 function getCallback(callbackName) {
21 return () => {
22 counter += 1;
23 commonCounter += 1;
24
25 console.log(`showFullscreenAdv; callback ${callbackName}; ${counter} call`);
26 }
27 }
28
29 ysdk.adv.showFullscreenAdv({
30 callbacks: {
31 onClose: getCallback('onClose'),
32 onOpen: getCallback('onOpen'),
33 onError: getCallback('onError')
34 }
35 });
36 });
37 });
38 </script>
39 </head>
40 <body>
41 <button id="button">显示广告</button>
42 </body>
43 </html>
示例细节:
-
为第一次广告调用设置了 onClose 回调函数。
-
为第二次调用和后续每次调用指定了每个可能的回调函数。
-
在广告块关闭后运行的代码将添加到
onClose回调函数中。 -
SDK 或回调函数造成的所有错误都会传递给 onError 函数。
-
为显示广告按钮分配了
'click'事件句柄(在每次点击该按钮时调用广告)。
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <meta charset="UTF-8">
5 <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
6 <meta name="mobile-web-app-capable" content="yes">
7 <meta name="apple-mobile-web-app-capable" content="yes">
8 <title>SDK 异步配置的页面示例</title>
9 <script>
10 let ysdk;
11
12 function initSDK() {
13 YaGames
14 .init()
15 .then(ysdk_ => {
16 ysdk = ysdk_;
17 ysdk.adv.showFullscreenAdv({
18 callbacks: {
19 onClose: wasShown => {
20 console.info('First close')
21 }
22 }
23 });
24 })
25 }
26
27 document.addEventListener('DOMContentLoaded', () => {
28 const buttonElem = document.querySelector('#button');
29
30 let commonCounter = 0;
31 buttonElem.addEventListener('click', () => {
32 let counter = 0;
33
34 function getCallback(callbackName) {
35 return () => {
36 counter += 1;
37 commonCounter += 1;
38
39 if (commonCounter % 3 === 0) {
40 throw new Error(`Test error in ${callbackName}, everything okey, it should not abort other code execution`);
41 }
42
43 console.info(`showFullscreenAdv; callback ${callbackName}; ${counter} call`);
44 }
45 }
46
47 function makeSomethingImportant() {
48 console.info('It\'s very important \'console.info\'');
49 }
50
51 if (ysdk) {
52 ysdk.adv.showFullscreenAdv({
53 callbacks: {
54 onClose: makeSomethingImportant,
55 onOpen: getCallback('onOpen'),
56 onError: function(error) {
57 console.error(error);
58 }
59 }
60 });
61 } else {
62 makeSomethingImportant();
63 }
64 });
65 });
66 </script>
67 </head>
68 <body>
69 <!-- Yandex Games SDK -->
70 <script>
71 (function(d) {
72 var t = d.getElementsByTagName('script')[0];
73 var s = d.createElement('script');
74 s.src = '/sdk.js';
75 s.async = true;
76 t.parentNode.insertBefore(s, t);
77 s.onload = initSDK;
78 })(document);
79 </script>
80 <button id="button">显示广告</button>
81 </body>
82 </html>
onClose:在广告关闭、发生错误或广告因调用过于频繁而未能打开的时候调用。与wasShown参数(boolean类型)一起使用,它的值表示是否显示广告。onOpen:在成功打开广告的时候调用。onError:在发生错误的时候调用。错误对象会传递给回调函数。
onClose:在广告关闭、发生错误或广告因调用过于频繁而未能打开的时候调用。与 wasShown 参数(boolean 类型)一起使用,它的值表示是否显示广告。
onError:在发生错误的时候调用。错误对象会传递给回调函数。
onOpen:在成功打开广告的时候调用。
这篇文章是否有用?
上一篇
下一篇