急求:循环发送xmlhttprequest,我头大了

ASPNewplayer 2006-06-07 12:58:21
我的aspx页中有这么一段ajax代码,用以循环发送xmlhttprequest
for(n=1;n<3;n++){
url=address.value+n;
alert(n);//如果在这里加入alert(n),代码首次运行勉强正确
sendRequest(url);//传入网址.这个函数里也有一个alert
}
第一种情况:没有alert(n);这一行
首次运行时,遇行总不正确,只执行n=2时的sendRequest,但在以后的运行中都能正确执行循环发送.


第二种情况:有alert(n);这一行
首次运行勉强正确...观察运行情况:
消息框弹出的顺序为:消息框1-->消息框2-->sendRequest1-->sendRequest2
在以后运行时,弹出的消息框顺序才会变得正确,消息框1-->sendRequest1-->消息框2-->sendRequest2

谁能帮我弄清这个问题啊
...全文
229 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
ASPNewplayer 2006-06-07
  • 打赏
  • 举报
回复
谢谢你的代码提醒了我,我用setTimeout("递归函数",2000)弄出来了,汗啊,这个setTimeout挺简单,用好了还真不容易....接分
crazor 2006-06-07
  • 打赏
  • 举报
回复
因为for循环太快了,sendRequest方法往往来不及取到值,加上alert等于暂时中断;

提供一个方法参考:
<script language="javascript">

var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
var tmp_str;
var continue_flag=0;

//延迟程序
function get_task_list(){
if(!continue_flag){
xmlhttp.open("POST",'MyCareer/xmlhttp_check_has_pop_info.asp?random_id='+Math.random(),true);
xmlhttp.send();
continue_flag=1
setTimeout("get_task_list()",2000)
}
else {
continue_flag=0
tmp_str=unescape(xmlhttp.responseText);
//alert(tmp_str);
//alert(FrameMain.mainform.test.value);
//FrameMain.location="MyCareer/TaskManage.asp";
xmlhttp.abort();
}
}

var timer=window.setInterval("get_task_list()", 6000);
window.onload=get_task_list;

function stop(){ window.clearInterval(timer);}

</script>

87,910

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧