0

如果页面加载超过 3 秒,则需要在页面加载之前显示一个弹出窗口。在代码下方使用,但如果页面加载也少于 tahn 3 seocnds,它会显示弹出窗口。如果页面加载需要更多时间而不是更少时间,则需要显示弹出窗口。

<script type="text/javascript">
    setTimeout(fnShowPopup, 1000);
    function fnShowPopup() {
        var answer = confirm("It may take few time to open this docuemnt. Click YES if you want to open the docuemnt in native format or click on CANCEL to continue viewing the docuemnt")
        if (answer)
            window.open(NativeView())
  }
</script>
4

1 回答 1

1

setTimeout(func, delay)附带一个中止计时器的方法:clearTimeout(timeoutID)

<script>
var myTimer = setTimeout(fnShowPopup, 3000);
if (typeof(window.addEventListener) === 'function') {
    // standard conforming browsers
    window.addEventListener('load', function () {
        clearTimeout(myTimer);
    }, true);
} else {
    // legacy, IE8 and less
    window.attachEvent('onload', function () {
        clearTimeout(myTimer);
    });
}
</script>

把它放在<head>你的页面中,在任何其他<script>s、<style>s 或<link>s 之前。

在您的 fnShowPopup 函数中,如果用户选择“本机格式”,您可能希望停止页面加载。见https://stackoverflow.com/a/10415265/

于 2013-04-25T10:46:41.913 回答