闭包及应用以及顺序处理ajax请求

前端开发必须知道的JS(二) 闭包及应用

function ajax() {
            var fns = arguments[0], backup = fns.concat();
 
            (function doit(fn) {
                $.get(fn.url,fn.paramMap, function(result) {
                    fn.call(this, result);
                    fns.shift();
                    if (fns.length > 0)
                        doit(fns[0]);
                });
            })(fns[0]);
        }
 
        (function() {
            document.getElementById("div_msg").innerHTML = 'waiting for 2 second<br>';
            var one = function() {
                document.getElementById("div_msg").innerHTML = document.getElementById("div_msg").innerHTML + arguments[0] + ('11<br>');
            };
            var two = function() {
                document.getElementById("div_msg").innerHTML = document.getElementById("div_msg").innerHTML + arguments[0] + ('22<br>');
            }
            var three = function() {
                document.getElementById("div_msg").innerHTML = document.getElementById("div_msg").innerHTML + arguments[0] + ('33<br>');
            }
 
            one.url = "handler1.ashx";
            two.url = "handler1.ashx"
            three.url = "handler1.ashx";
            one.paramMap = { actionType: "one" }
            two.paramMap = { actionType: "two" }
            three.paramMap = { actionType: "three" }
 
 
            var fns =
            [
             one,
                two,
                three
            ];
            ajax(fns);
        })();
public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.AddHeader("Cache-Control"," no-cache, must-revalidate"); ;
            System.Threading.Thread.Sleep(2000);
            switch(context.Request["actiontype"]){
                case "one":
                    context.Response.Write("111111111");
                    break;
                case "two":
                    context.Response.Write("222222222");
                    break;
                case "three":
                    context.Response.Write("333333333");
                    break;
            }
        }
原文地址:https://www.cnblogs.com/jianjialin/p/1771918.html