日期:2014-05-16  浏览次数:20387 次

关于setTimeout clearTimeout 延时循环中只能用alert显示循环过程,不能用document.write
这是一个用延时循环刷新另一个页面的例子,用以下方法可以实现,
但只能用alert得到循环的过程,用document.write 就不可以,不知道为什么,找了很多资料也没弄明白
希望高人给点指点,迫不得已麻烦各位,谢谢
alert(WidthP+":"+url); //这里用alert就可以正常运行,用document.write或者document.其他方法都不行,请问这是为什么呢

JScript code


<script language="javascript">
var timeUp = 0;
var NewsID = "<%=SeleceNewsID%>".split(",");
delayRun();


//循环执行任务,延时操作
function delayRun(code,time){

url='http://localhost/keywordsget1.asp?NewsID='+NewsID[timeUp];
validateEmail(url);

var WidthP = timeUp/NewsID.length;
WidthP = Math.round(WidthP * 100)+"%";


[color=#FF0000]alert(WidthP+":"+url);[/color] //这里用alert就可以正常运行,用document.write或者document.其他方法都不行,请问这是为什么呢


if(timeUp < NewsID.length){
timeUp++;
var t = setTimeout(function(){delayRun(code,time);},time);
}else{
   clearTimeout(t);
}
}



//XMLHTTP提交数据并获取网页内容
var xmlHttp;
 function validateEmail(url) {

  
  if (window.ActiveXObject) {
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  }
  else if (window.XMLHttpRequest) {
    xmlHttp = new XMLHttpRequest();
  }
 xmlHttp.open("GET", url);
 xmlHttp.onreadystatechange = callback;
 xmlHttp.send(null);
 }
 
 function callback() {
  if (xmlHttp.readyState == 4) {
    if (xmlHttp.status == 200) {
            

    }
  }
 }
</script>




------解决方案--------------------
document.write 是将内容输入到网页中

执行后,页面中当然就没有以前写的程序代码了,自然也就不执行了.


------解决方案--------------------
用$(id).innerHTML = 'xxxx'输出。
------解决方案--------------------
在页面加载完毕的情况下,使用document.write会清空网页的内容的。。。。