h5唤起APP并检查是否成功

 1 // 检查app是否打开
 2 function checkOpen(cb) {
 3     const clickTime = +(new Date());
 4     function check(elsTime) {
 5         if (elsTime > 3000 || document.hidden || document.webkitHidden) {
 6             cb(true);
 7         } else {
 8             cb(false);
 9         }
10     }
11     // 启动间隔20ms运行的定时器,并检测累计消耗时间是否超过3000ms,超过则结束
12     let count = 0;
13     let intHandle = null;
14     intHandle = setInterval(() => {
15         count++;
16         const elsTime = +(new Date()) - clickTime;
17         if (count >= 100 || elsTime > 3000) {
18             clearInterval(intHandle);
19             check(elsTime);
20         }
21     }, 20);
22 }
23 /**
24  * 唤起app
25  * @param {any} uri  app schema
26  * @param {any} fallback  唤起失败回调
27  */
28 function openApp(uri, fallback) {
29     const ifr = document.createElement('iframe');
30     ifr.src = uri;
31     ifr.style.display = 'none';
32     if (fallback) {
33         checkOpen((opened) => {
34             if (!opened) {
35                 fallback();
36             }
37         });
38     }
39     document.body.appendChild(ifr);
40     setTimeout(() => {
41         document.body.removeChild(ifr);
42     }, 2000);
43 }

 

posted @ 2018-08-07 14:15  Shoestrong  阅读(764)  评论(0编辑  收藏  举报
更多精彩请访问个人主页http://shoestrong.cc