2015-04-23 80 views
0

嘿家伙是新来的ajax和PHP ..我有一个PHP脚本显示两位数字1和2 ..我需要的是我只想显示数字1 3秒后和在3秒后显示其他数字2,我想清除intervel..This是我试图..settimeout显示逻辑错误

Ajax.html
<!DOCTYPE html> 
<html> 
<head> 
</head> 
<body> 
<script> 
var d = setInterval(function babe() { 

var xmlhttp = new XMLHttpRequest(); 
xmlhttp.onload = function() { 
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
     var str = xmlhttp.responseText; 
    var res = str.split(""); 
console.log(res[0]); 


     setInterval(function() { console.log(res[2]); }, 5555); 
    } 
} 




xmlhttp.open("GET", "index.php", true); 
xmlhttp.send(); 


setInterval(function() { clearInterval(d);}, 3000); 
}, 3000); 
</script> 
</body> 
</html> 

此代码输出1两次并将其汽车无执行2没有结束。

我需要的是执行后3秒后3秒12并清除间隔..

thanx的帮助乡亲.. :)

+0

无法理解你的代码......太多的功能函数...你至少缩进它吗?在这里,我看到在'setInterval(...)'函数内调用'setInterval(...)'看起来像是有问题.... – Random

回答

0

的setInterval不断为performc

setInterval(function() { console.log(res[2]); }, 5555); 

不断将执行2

你可以尝试

setTimeout(function() { console.log(res[2]); }, 3000); 

密切d后3秒

setTimeout(function() { clearInterval(d);}, 3000); 
+0

不,它没有帮助我 –