ajax轮询

来源:互联网 发布:windows cmd telnet 编辑:程序博客网 时间:2024/06/17 09:09

原文链接:http://www.cnblogs.com/hoojo/p/longPolling_comet_jquery_iframe_ajax.html

代码段:        $(function () {            (function longPolling() {                $.ajax({                    url: "${pageContext.request.contextPath}/communication/user/ajax.mvc",                    data: {"timed": new Date().getTime()},                    dataType: "text",                    timeout: 5000,                    error: function (XMLHttpRequest, textStatus, errorThrown) {                        $("#state").append("[state: " + textStatus + ", error: " + errorThrown + " ]<br/>");                        if (textStatus == "timeout") { // 请求超时                            longPolling(); // 递归调用                            // 其他错误,如网络错误等                        } else {                            longPolling();                        }                    },                    success: function (data, textStatus) {                        $("#state").append("[state: " + textStatus + ", data: { " + data + "} ]<br/>");                        if (textStatus == "success") { // 请求成功                            longPolling();                        }                    }                });            })();            });
0 0
原创粉丝点击