2013-03-13 178 views
2

async=falseasync=true有什么区别,当我用open的方法XMLHttpRequest同步请求和异步请求之间有什么区别? (async = true/false)

function GetXML() { 

    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari 
     xmlhttp = new XMLHttpRequest(); 
    } 
    else {// code for IE6, IE5 
     xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    xmlhttp.onreadystatechange = test 
    xmlhttp.open("GET", "PlanetCafe.xml", true); 
    xmlhttp.send(null); 
} 

回答

8

Mozilla Developer: Synchronous and Asynchronous Requests

XMLHttpRequest支持同步和异步通信。但是,通常,出于性能原因,异步请求应该优先于同步请求。

简而言之,同步请求会阻止代码的执行,并可能泄漏内存和事件。这可能会导致严重问题。使用同步请求的唯一可行理由是为了更方便地在Web Workers之内便于下载。