开发者

PHP/JS ? Website: We have lost connection

开发者 https://www.devze.com 2022-12-29 07:23 出处:网络
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\

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

开发者_如何学Python

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号