zoukankan      html  css  js  c++  java
  • Jquery中Ajax异步请求中的async参数的作用

    之前不知道这个参数的作用,上网找了前辈的博客,在此收录到自己的博客,希望能帮到更多的朋友:

    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后才赋值的,结果,输出时会为空。

     

  • 相关阅读:
    [Jobdu] 题目1528:最长回文子串
    [Jobdu] 题目1510:替换空格
    [Leetcode] Candy
    [Leetcode] Jump Game
    [Leetcode] Longest Valid Parentheses
    [Leetcode] Triangle
    [Leetcode] Populating Next Right Pointers in Each Node
    java web作用域page request session application
    String对象不可改变的特性及内存机制
    Java IO
  • 原文地址:https://www.cnblogs.com/ChinazhouWang/p/5096695.html
Copyright © 2011-2022 走看看