深刻理解ajax中同步和异步的区别和使用场景

先上两段代码,猜下这两段代码的console.log的执行顺序和结果:

代码一:

                    $.ajax({
                        type: "post",
                        async:true, //不设置此参数的话默认为true
                        url: "/teacher/ajaxtimeline",
                        data: {"month": month, "uid":<?php echo intval($teacher->uid);?>},
                        dataType: "json",
                        success: function(data) {
                            //假设data.canNotSetDays服务端返回的是[9,10,11]
                            $.each(data.canNotSetDays, function(i, item) {
                                canNotSetDays.push(item);
                            });

                            console.log('**********ajax result ************');
                            //此处是可以获取canNotSetDays的值的,并能查找到其中的元素数字9
                            console.log(canNotSetDays);
                            console.log(canNotSetDays.length);
                            console.log(canNotSetDays.indexOf(9));

                        }
                    });

                    console.log('***********after ajax ***********');

                    //此处是获取不到canNotSetDays的值的,长度为0,无法查找到其中的元素数字9
                    console.log(canNotSetDays);
                    console.log(canNotSetDays.length);
                    console.log(canNotSetDays.indexOf(9));

                    console.log('**********delay two seconds************');

                    setTimeout(function(){
                        //延迟2秒后也是可以获取canNotSetDays的值的,并能查找到其中的元素数字9
                        console.log(canNotSetDays);
                        console.log(canNotSetDays.length);
                        console.log(canNotSetDays.indexOf(9));
                    }, 2000);
            

代码二:

                     $.ajax({
                        type: "post",
                        async:false, //不设置此参数的话默认为true
                        url: "/teacher/ajaxtimeline",
                        data: {"month": month, "uid":<?php echo intval($teacher->uid);?>},
                        dataType: "json",
                        success: function(data) {
                            //假设data.canNotSetDays服务端返回的是[9,10,11]
                            $.each(data.canNotSetDays, function(i, item) {
                                canNotSetDays.push(item);
                            });

                            console.log('**********ajax result ************');
                            //此处是可以获取canNotSetDays的值的,并能查找到其中的元素数字9
                            console.log(canNotSetDays);
                            console.log(canNotSetDays.length);
                            console.log(canNotSetDays.indexOf(9));

                        }
                    });

                    console.log('***********after ajax ***********');

                    //此处是可以获取canNotSetDays的值的,并能查找到其中的元素数字9
                    console.log(canNotSetDays);
                    console.log(canNotSetDays.length);
                    console.log(canNotSetDays.indexOf(9));

                    console.log('**********delay two seconds************');

                    setTimeout(function(){
                        //延迟2秒后肯定也是可以获取canNotSetDays的值的,并能查找到其中的元素数字9
                        console.log(canNotSetDays);
                        console.log(canNotSetDays.length);
                        console.log(canNotSetDays.indexOf(9));
                    }, 2000);



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值