页面离开之前要执行的事件

33 阅读1分钟

需求:在浏览器需要打开app内具体的某个页面 浏览器无法判断当前手机是否安装了某个app,所以想通过定时器的方式处理,如果2秒内没有打开也去到 app store 去下载

const onDownload = () => {
    if (hl.isweixin) {
        showMask.value = true;
        return
    }


    window.location.href = setD.extinfo;
    const timer = setTimeout(() => {
        console.log('下载超时,跳转下载地址');
        // showMask.value = false;
        if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
            // window.location.href ="https://testflight.apple.com/join/ai3YRycZ"
            window.location.href = "https://apps.apple.com/cn/app/%E6%AC%A2%E4%B9%90%E5%8F%8B%E9%81%93/id6461309385"
        } else {
            window.location.href = "https://home.huanleyoudao.com/download-apk/huanleyoudao.apk"
        }
    }, 2000);

    window.onbeforeunload = function () {
        console.log('关闭窗口前执行');
        clearTimeout(timer);
    }

}