Skip to content

Instantly share code, notes, and snippets.

@CooLNuanfeng
Created November 23, 2016 09:17
Show Gist options
  • Save CooLNuanfeng/1f9085ae86b844f80adb81df85428326 to your computer and use it in GitHub Desktop.
Save CooLNuanfeng/1f9085ae86b844f80adb81df85428326 to your computer and use it in GitHub Desktop.
JavaScript 拉起 App
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