Last active
January 22, 2018 03:30
-
-
Save blackmiaool/38979d5cd2921a55f090341923a795ce to your computer and use it in GitHub Desktop.
try to open 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 checkOpen() { | |
const clickTime = Date.now(); | |
let timeout; | |
let count = 0; | |
return new Promise((resolve, reject) => { | |
const interval = setInterval(() => { | |
count++; | |
timeout = Date.now() - clickTime > 3000; | |
if (count >= 10 || timeout) { | |
clearInterval(interval); | |
if (timeout || document.hidden || document.webkitHidden) { | |
resolve(true); | |
} else { | |
resolve(false); | |
} | |
} | |
}, 200); | |
}); | |
} | |
/** | |
* 尝试唤起app | |
* @param targetUrl 需要打开的url,可以以https?开头 | |
* @returns {Promise} 返回是否成功唤起的promise | |
*/ | |
function tryOpenApp(targetUrl = 'youpin://mijiayoupin.com/shop/main') { | |
targetUrl = targetUrl.replace(/https?:\/\//, "youpin://"); | |
const ifr = document.createElement('iframe'); | |
ifr.src = targetUrl; | |
ifr.style.display = 'none'; | |
document.body.appendChild(ifr); | |
setTimeout(() => { | |
document.body.removeChild(ifr); | |
}, 2000); | |
return checkOpen(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment