如果加载页面的时间超过3秒,则显示弹出窗口

时间:2013-04-25 10:36:28

标签: javascript

如果页面加载超过3秒,则需要在页面加载前显示弹出窗口。在代码下方使用但如果页面加载少于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>

1 个答案:

答案 0 :(得分: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><style><link>之前。

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