ajax同步非同步

來源:互聯網
上載者:User

標籤:輸出   href   end   資料   瀏覽器   async   als   type   執行   

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同步非同步

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.