Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Dec 2010
    Posts
    27
    Thanks
    1
    Thanked 0 Times in 0 Posts

    window unload event not firing

    hi,
    i have aded window unload event handler to my code. my page calls some function that takes few secs and page then loads in 5-6 secs. while ths page is loading, if user clicks 'x' on browser window, then code inside unload event handler doesnt get executed. if user waits for page to be loaded fully and hten clicks 'x', then unload event handler gets executed. but i want if during loading user decides to click 'x' button, then also unload handler shold get exucted. i am pasting a sample code that simulates my situation:

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
    <title>Untitled Page</title>
    <script type="text/javascript">
    function unloadPage() {
    alert('unloaded page');
    }
    function pauseJS(timeInMilliS) {
    var date = new Date();
    var curDate = null;
    do {
    curDate = new Date();
    }while(curDate-date < timeInMilliS);
    }
    </script>
    </head>
    <body onload="pauseJS(6000)" onunload="unloadPage()">
    <p>Test Page</p>
    </body>
    </html>

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,553
    Thanks
    78
    Thanked 4,382 Times in 4,347 Posts
    That's because your pauseJS function is running for the entire 6 seconds and WHILE IT IS RUNNING IT LOCKS OUT EVERYTHING ELSE.

    JavaScript is SINGLE-THREADED, meaning that any events that happen while JS code is running will simply wait until the JS code is finished.

    You will have to find a way to allow the events to happen. Perhaps break your 6-second job into quarter-second pieces with a short window.timeout() in between each?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •