示例
以下示例展示如何使用同步或异步方法配置 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()
12 .then((ysdk) => {
13 ysdk.adv.showFullscreenAdv();
14
15 const buttonElem = document.querySelector('#button');
16
17 let commonCounter = 0;
18 buttonElem.addEventListener('click', () => {
19 let counter = 0;
20
21 function getCallback(callbackName) {
22 return () => {
23 counter += 1;
24 commonCounter += 1;
25
26 console.log(`showFullscreenAdv; callback ${callbackName}; ${counter} call`);
27 };
28 }
29
30 ysdk.adv.showFullscreenAdv({
31 callbacks: {
32 onClose: getCallback('onClose'),
33 onOpen: getCallback('onOpen'),
34 onError: getCallback('onError')
35 }
36 });
37 });
38 });
39 </script>
40 </head>
41 <body>
42 <button id="button">显示广告</button>
43 </body>
44</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.init()
14 .then((ysdk_) => {
15 ysdk = ysdk_;
16 ysdk.adv.showFullscreenAdv({
17 callbacks: {
18 onClose: (wasShown) => {
19 console.info('First close');
20 }
21 }
22 });
23 });
24 }
25
26 document.addEventListener('DOMContentLoaded', () => {
27 const buttonElem = document.querySelector('#button');
28
29 let commonCounter = 0;
30 buttonElem.addEventListener('click', () => {
31 let counter = 0;
32
33 function getCallback(callbackName) {
34 return () => {
35 counter += 1;
36 commonCounter += 1;
37
38 if (commonCounter % 3 === 0) {
39 throw new Error(`Test error in ${callbackName}, everything okay, it should not abort other code execution`);
40 }
41
42 console.info(`showFullscreenAdv; callback ${callbackName}; ${counter} call`);
43 };
44 }
45
46 function makeSomethingImportant() {
47 console.info('It\'s very important \'console.info\'');
48 }
49
50 if (ysdk) {
51 ysdk.adv.showFullscreenAdv({
52 callbacks: {
53 onClose: makeSomethingImportant,
54 onOpen: getCallback('onOpen'),
55 onError: function (error) {
56 console.error(error);
57 }
58 }
59 });
60 } else {
61 makeSomethingImportant();
62 }
63 });
64 });
65 </script>
66 </head>
67 <body>
68 <!-- Yandex Games SDK -->
69 <script>
70 (function (d) {
71 var t = d.getElementsByTagName('script')[0];
72 var s = d.createElement('script');
73 s.src = '/sdk.js';
74 s.async = true;
75 t.parentNode.insertBefore(s, t);
76 s.onload = initSDK;
77 })(document);
78 </script>
79 <button id="button">显示广告</button>
80 </body>
81</html>
onClose:在广告关闭、发生错误或广告因调用过于频繁而未能打开的时候调用。与wasShown参数(boolean类型)一起使用,它的值表示是否显示广告。onOpen:在成功打开广告的时候调用。onError:在发生错误的时候调用。错误对象会传递给回调函数。
onClose:在广告关闭、发生错误或广告因调用过于频繁而未能打开的时候调用。与 wasShown 参数(boolean 类型)一起使用,它的值表示是否显示广告。
onError:在发生错误的时候调用。错误对象会传递给回调函数。
onOpen:在成功打开广告的时候调用。
这篇文章是否有用?
上一篇
下一篇