2013-04-25 131 views
0

如果页面加载时间超过3秒,则需要在页面加载前显示弹出窗口。在下面的代码中使用,但是如果页面加载的时间太少,也会显示弹出窗口3 seocnds。如果页面加载需要更多的时间而不是更少的时间,Popup需要显示。如果页面加载时间超过3秒,则显示弹出窗口

<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

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>秒。

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

+0

获取 “微软JScript运行时错误:对象不支持此属性或方法” 在低于行:window.addEventListener( '负载',函数(){clearTimeout(myTimer);},真) – 2013-04-25 11:16:31

+0

嗯,我以为我的祷告被听到了,IE8也绝迹了。请参阅http://stackoverflow.com/questions/6927637/addeventlistener-in-internet-explorer – kay 2013-04-25 11:23:55

+0

对于上面的代码,弹出窗口在页面加载后显示。我的要求是,我有一个链接的IE浏览器,如果我点击一个链接,它会打开一个新的IE浏览器,这就像文档查看器,我们可以注释保存打印电子邮件的文档等。例如,可以有3页或100页甚至500至1000页的文件。大文件将需要更多时间来加载文件。那时需要显示一个弹出窗口。对于上述示例,即使对于小文档和大文档,页面加载后也会显示弹出窗口。 – 2013-04-25 14:13:40

相关问题