SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Nov 2010
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Detecting connection loss

    Hi all,

    I have a script which needs to detect when the internet connection is lost and hold the content of the web page until the connection is restored.

    I have been given a head start with a small javascript which I am trying to get to work.

    My web page, I have a web page which contains some text and graphics but more importantly it also has an iFrame. The iFrame connects to an SQL database and displays data which is refreshed every 20 seconds. Because the internet connection at the location where the script will be running I get connection loss which makes the iFrame loose its data. The Javascript I am working with is below, can anyone see why it should not work and give a hand in solving the problem.

    Code:
    var hasConnectionBeenLost = false;
    (function ( img, wait, notify )
    {
        var iObj = new Image();
    
        iObj.onerror = function ()
        {
            if ( notify )
        {
                alert( 'reloading' );
                hasConnectionBeenLost = true;
                setTimeout( test, wait );
                //display some message to user connection is lost
                //document.getElementById("someElementMessage").style.display="block";
            }
    
            iObj.onload = function ()
            {
                if (hasConnectionBeenLost)
                {
                    location.reload( true );
                } 
        		else 
        		{
                    setTimeout( test, wait );
                }
            };
    
     
    
            function test()
       			{
                iObj.src = img + '?t=' + new Date();
         	   }
    
            test();
    
        }( 'images/myimage.gif', 80000, false );
    }
    );
    Many thanks in advance.

    Best regards,

    Bytec
    Last edited by bytec; Nov 27, 2010 at 11:57. Reason: Spelling errror

  2. #2
    Under Construction silver trophybronze trophy AussieJohn's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    776
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    Just as an aside, there are a few other methods to this sort of thing as well, I found a post on StackOverflow that describes a few methods and there is even a tiny jQuery plugin to do this.

    As far as your code is concerned, you seem to be using a JavaScript closure that's not executing itself at the end...

    Try moving the "( 'images/myimage.gif', 80000, false )" to the last line, as per below:

    Code JavaScript:
    var hasConnectionBeenLost = false;
    (function ( img, wait, notify )
    {
        var iObj = new Image();
        iObj.onerror = function ()
        {
            if ( notify )
        {
                alert( 'reloading' );
                hasConnectionBeenLost = true;
                setTimeout( test, wait );
                //display some message to user connection is lost
                //document.getElementById("someElementMessage").style.display="block";
            }
     
            iObj.onload = function ()
            {
                if (hasConnectionBeenLost)
                {
                    location.reload( true );
                } 
        		else 
        		{
                    setTimeout( test, wait );
                }
            };
     
     
     
            function test()
       			{
                iObj.src = img + '?t=' + new Date();
         	   }
     
            test();
     
        }
    }
    //make sure the closure executes
    )( 'images/myimage.gif', 80000, false );
    var details = {
    . . web: "afterlight.com.au",
    . . photos: "jvdl.id.au",
    . . psa: "usethelatestversion.com"
    }


Bookmarks

Posting Permissions

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