Why should I reuse XmlHttpRequest objects?
Ajax-intensive page: reuse the same XMLHttpRequest object or create new one every time?
另外,有一个关于w3schools.com的建议:
If you have more than one AJAX task on your website,you should create
ONE standard function for creating the XMLHttpRequest object,and call
this for each AJAX task.
If you have more than one AJAX task on your website,you should create ONE standard function for creating the XMLHttpRequest object,and call this for each AJAX task.
它表示您使用一个AJAX函数来获取请求。此功能将处理IE和其他浏览器之间的不一致。符合标准的浏览器中的XMLHttpRequest,以及IE中的ActiveXObject。
我建议使用多个XHR对象。使用一个全局xhr对象,您的应用程序只能在给定时间处理一个请求。这也很容易出错(例如XMLHttpRequest launches multiple times without initiating the onreadystatechange function)。
W3schools的意思是:
function createXHR() { try { return new XMLHttpRequest(); } catch (e) { try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { return new ActiveXObject("Msxml2.XMLHTTP"); } } } var xhr = createXHR(); xhr.open('get','/test',true); xhr.send();
虽然最好创建一个处理请求的函数,如jQuery.ajax
。