Ajax同一页面同时执行多个XMLHTTP (1)

[color=red]1.第二个请求延时几秒执行,可避免第一个请求还没执行完就被覆盖[/color]
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>AJAX同一页面加载多个测试</title>
<script language="JavaScript">
var xmlHttp;
function createXMLHttpRequest() {
if (window.XMLHttpRequest){ //FireFox
xmlHttp = new XMLHttpRequest();
}
else if(window.ActiveXObject){
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); //IE6
}catch(e){
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); //IE7
}catch (e){
alert('对不起,本页面只支持IE6,IE7,FireFox');
}
}
}
}
function startRequest(tn,what) {
createXMLHttpRequest();
if(what=='msg1'){
alert('第一次开始了');
}else{
alert('第二次开始了');
}
xmlHttp.onreadystatechange = function(){handleStateChange(what);};
xmlHttp.open("GET", "test2.jsp?tn="+tn, true);
xmlHttp.send(null);
}
function handleStateChange(what) {
if(xmlHttp.readyState == 4) {
if(xmlHttp.status == 200) {
if(what=='msg1'){
alert('第一次回调');
}else{
alert('第二次回调');
}
var result = xmlHttp.responseText;
document.getElementById(what).innerHTML = result;
}
}
else {
document.getElementById(what).innerHTML = "正在载入...";
}
}
function myload(){
startRequest('sort_jiaoyi','msg1');
[color=red] setTimeout("startRequest('sort_fangwu','msg2')",2000); //延时2秒后执行,在IE7下竟然不需要延时处理...[/color]
}
</script>
</head>
<body onLoad="myload();">
<table>
<tr><td>
<div id="msg1" style="width:300px; background:#0099FF"></div>
</td></tr>
<tr><td>
<div id="msg2" style="width:300px; background:#66FF33"></div>
</td></tr>
</table>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值