Sravya Potturi
Sravya Potturi

Reputation: 1

Displaying popup window if it is taking more than 3 seconds to load a page

Need to Display a popup before page load if page is loading more than 3 seconds. Used below code but it displays popup if page load is less tahn 3 seocnds also. Popup need to displayed if the page loading takes more time not less time.

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

Upvotes: 0

Views: 1304

Answers (1)

Kijewski
Kijewski

Reputation: 26043

setTimeout(func, delay) comes with a method to abort the timer: 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>

Put this in the <head> of your page, before any other <script>s, <style>s or <link>s.

In your fnShowPopup function you may want to stop the page loading if the user chooses the "native format". See https://stackoverflow.com/a/10415265/

Upvotes: 1

Related Questions