2013-04-25 49 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

相關問題