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

ajax参数async:false/true的作用

时间:2018-05-18 14:04:50      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:结果   数据   异步   fan   赋值   https   oid   pre   log   

原文:https://blog.csdn.net/yanfangphp/article/details/8261333

 

test.html

<a href="javascript:void(0)" onmouseover="testAsync()">

asy.js

function testAsync(){
    var temp;
    $.ajax({
        async: false,
        type : "GET",
        url : ‘tet.php‘,
        complete: function(msg){
            alert(‘complete‘);
        },
        success : function(data) {
            alert(‘success‘);
            temp=data;
        }
    });
    alert(temp+‘   end‘);
}

tet.php

<?php

    echo "here is html code";
    sleep(5);

?>

async: false,(默认是true);
如上:false为同步,这个 testAsync()方法中的Ajax请求将整个浏览器锁死,
只有tet.php执行结束后,才可以执行其它操作。

当async: true 时,ajax请求是异步的。但是其中有个问题:testAsync()中的ajax请求和其后面的操作是异步执行的,那么当tet.php还未执行完,就可能已经执行了 ajax请求后面的操作,
如: alert(temp+‘   end‘);
然而,temp这个数据是在ajax请求success后才赋值的,结果,输出时会为空。

 

ajax参数async:false/true的作用

标签:结果   数据   异步   fan   赋值   https   oid   pre   log   

原文地址:https://www.cnblogs.com/pecool/p/9055543.html

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