js 唤端(js唤起手机App)

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

h5页面唤起app
const appDownload = () => { // 分享- 唤起 - 下载
	function failed() {
		window.location = '应用商店的scheme '
	}
	function transfer(cb) {
		window.location.href = '自家产品的scheme,需要与客户端同事定义'
		const initialTime = new Date();
		let counter = 0;
		let waitTime = 0;
		const checkOpen = setInterval(() => {
			counter++;
			waitTime = new Date() - initialTime;
			if (waitTime > 5000) {
				clearInterval(checkOpen);
				cb();
			}
			if (counter < 1000) { return; }
			const hide = document.hidden || document.webkitHidden;
			if (!hide) {
				cb(); // 唤端失败的回调函数
			}
		}, 20);
		document.addEventListener('visibilitychange', () => {
			const isHidden = document.hidden;
			if (isHidden) {
				clearInterval(checkOpen);
			}
		});
	}
	transfer(failed)
}

转载于:https://my.oschina.net/u/4052053/blog/3004704

你可能感兴趣的:(js 唤端(js唤起手机App))