码迷,mamicode.com
首页 > Web开发 > 详细

Ajax长轮询

时间:2014-11-26 13:56:52      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   os   sp   java   

前台代码:

bubuko.com,布布扣
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Wait.aspx.cs" Inherits="Web监听.Wait" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link href="/JS/easyui/easyui.css" rel="stylesheet" type="text/css" />
    <script src="/JS/jquery.min.js" type="text/javascript"></script>
    <script src="/JS/easyui/jquery.easyui.min.js" type="text/javascript"></script>
    <script src="/JS/SimpoWindow.js" type="text/javascript"></script>
    <script type="text/javascript">
        var longPollingCount;

        $(function () {
            longPollingCount = 1;
            $("#msg").html("" + longPollingCount + "次请求......");

            longPolling();
        });

        function longPolling() {
            $.ajax({
                type: "POST",
                url: "Check.aspx?action=check",
                dataType: "text",
                timeout: 5000,
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    longPollingCount++;
                    $("#msg").html("" + longPollingCount + "次请求......");

                    if (textStatus == "timeout") { // 请求超时
                        longPolling(); // 递归调用
                    } else { // 其他错误,如网络错误等
                        $("#msg").html(textStatus);
                        longPolling();
                    }
                },
                success: function (data, textStatus) {
                    var parent = SimpoWin.GetWinParent();
                    $("#txt").html("操作完成......");
                    $("#txt").css("color", "red");

                    setTimeout(function () {
                        SimpoWin.closeWin();
                        parent.next(data);
                    }, 1000);

                }
            });
        }
    </script>
</head>
<body style="background-color: White;">
    <form id="form1" runat="server">
    <div id="txt" style="text-align: center; vertical-align: middle; margin-top: 20px;
        margin-bottom: 20px; font-weight: bold;">
        请等待......
    </div>
    <div id="msg" style="text-align: center; vertical-align: middle;">
    </div>
    </form>
</body>
</html>
View Code

后台代码:

bubuko.com,布布扣
using System;
using System.Threading;

namespace Web监听
{
    public partial class Check : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string action = Request["action"];
            switch (action)
            {
                case "check":
                    while (true)
                    {
                        if (Common.flag < 160)
                        {
                            Common.flag++;
                            Thread.Sleep(100);
                        }
                        else
                        {
                            Response.Write("操作完成返回信息");
                            Response.End();
                            break;
                        }
                    }
                    break;
            }
        }
    }
}
View Code

 

Ajax长轮询

标签:style   blog   http   io   ar   color   os   sp   java   

原文地址:http://www.cnblogs.com/s0611163/p/4122911.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!