Created
November 23, 2016 09:16
-
-
Save CooLNuanfeng/9cc202d060c8835a463c52a541048cf3 to your computer and use it in GitHub Desktop.
JavaScript 拉起 App
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function openApp(openUrl, callback) { | |
//检查app是否打开 | |
function checkOpen(cb){ | |
var _clickTime = +(new Date()); | |
function check(elsTime) { | |
if ( elsTime > 7000) { // 7000 = 5000 + toast的 2000 | |
cb(1); | |
} else { | |
cb(0); | |
} | |
} | |
//启动间隔20ms运行的定时器,并检测累计消耗时间是否超过3000ms,超过则结束 | |
var _count = 0, intHandle; | |
intHandle = setInterval(function(){ | |
_count++; | |
var elsTime = +(new Date()) - _clickTime; | |
if (_count>=250 || elsTime > 5000 ) { | |
clearInterval(intHandle); | |
check(elsTime); | |
} | |
}, 20); | |
} | |
//在iframe 中打开APP | |
var ifr = document.createElement('iframe'); | |
ifr.src = openUrl; | |
ifr.style.display = 'none'; | |
if (callback) { | |
checkOpen(function(opened){ | |
callback && callback(opened); | |
}); | |
} | |
document.body.appendChild(ifr); | |
setTimeout(function() { | |
document.body.removeChild(ifr); | |
}, 2000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment