【问题标题】:Displaying popup window if it is taking more than 3 seconds to load a page如果加载页面的时间超过 3 秒,则显示弹出窗口
【发布时间】:2013-04-25 19:25:10
【问题描述】:

如果页面加载超过 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>

【问题讨论】:

    标签: javascript


    【解决方案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>
    

    将此放在您页面的&lt;head&gt; 中,在任何其他&lt;script&gt;s、&lt;style&gt;s 或&lt;link&gt;s 之前。

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

    【讨论】:

    • 在以下行中获取“Microsoft JScript 运行时错误:对象不支持此属性或方法”:window.addEventListener('load', function () { clearTimeout(myTimer); }, true)
    • 嗯,我以为我的祈祷被听到了,IE8 已经灭绝了。请看stackoverflow.com/questions/6927637/…
    • 对于上面的代码,页面加载后弹出窗口正在显示。我的要求是我有一个带有链接的 IE,如果我点击一个链接,它将打开一个新的 IE,它就像一个文档查看器,我们可以在其中注释保存打印电子邮件文档等等。例如,可以有 3 页或 100 页甚至 500 到 1000 页的文档。大型文档将需要更多时间来加载文档。那时需要显示一个弹出窗口。对于上面的示例,即使对于小文档和大文档,页面加载后也会显示弹出窗口,其中包含一些控件。
    • 如果文档或查看器花费超过 3 秒而不是在 3 秒内加载的查看器,我需要显示弹出窗口。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-17
    • 1970-01-01
    • 1970-01-01
    • 2017-03-03
    • 2011-07-31
    相关资源
    最近更新 更多