2015-04-23 56 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

不,它沒有幫助我 –