views:

34

answers:

1

Hello.. like in facebook.com or in Gmail if you go offline/loose connection while your on the page, you will get "some went wrong, trying to connect in x:xx seconds" or like "you have lost your connection"..

how have they done so they can give an offline message? How to do that? example?

+2  A: 

Facebook and Gmail constantly poll the server with Ajax Requests in certain intervals. If the server doesn't respond in a certain time, they will give the error message. Simple as that.

Easy example:

window.setInterval(function() {
    try {
        myFavoriteAjaxLib.poll('/server/heartbeat/time/1234567890');
    } catch(e) {
        alert('Something went wrong');
    }
}, 5000);

Of course, you will have something more sophisticated in the real world. You will see the heartbeat thing quite often though. SO uses one too. Type the following into your browser's address bar:

 javascript:for(i in heartbeat) alert(i +':'+ heartbeat[i]);

or inspect the DOM with Firebug.

Gordon
+1 yep ........
Gaby