2011-12-14 9 views
2

xmlhttpリクエストを使用して2つのAJAXスクリプトがあります。 しかし、彼らはお互いに衝突しています。たとえば、AJAX関数2は関数1のxmlhttp.responseを表示します。私はこれを止める方法があるのだろうかと思っていたのですか?私はすでにxmlhttp.closeを追加しようとしましたが、うまくいきませんでした。 ありがとうございます!XMLHTTPが互いに衝突するのを止めるには?

<script>var hello = setInterval(function() 
{ 
if (window.XMLHttpRequest) 
    {// code for IE7+, Firefox, Chrome, Opera, Safari 
    xmlhttp=new XMLHttpRequest(); 
    } 
else 
    {// code for IE6, IE5 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
xmlhttp.onreadystatechange=function() 
    { 
    if (xmlhttp.readyState==4 && xmlhttp.status==200) 
    { 
    document.getElementById("main").innerHTML=xmlhttp.responseText; 
    xmlhttp.close; 
    } 
    } 
xmlhttp.open("GET","ajax.php?user=" +username,true); 
xmlhttp.send(); 
}, 1000); 
</script> 

<script>var anotherone = setInterval(function() 
{ 
if (window.XMLHttpRequest) 
    {// code for IE7+, Firefox, Chrome, Opera, Safari 
    xmlhttp=new XMLHttpRequest(); 
    } 
else 
    {// code for IE6, IE5 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
xmlhttp.onreadystatechange=function() 
    { 
    if (xmlhttp.readyState==4 && xmlhttp.status==200) 
    { 
    document.getElementById("sub").innerHTML=xmlhttp.responseText; 
    xmlhttp.close; 
    } 
    } 
xmlhttp.open("GET","ajax.php?user=" +username +"&do=" +option,true); 
xmlhttp.send(); 
}, 1000); 
</script> 

答えて

3

あなたは1つのXMLHttpRequestを書き留めておきます。各機能では、行で始まります。

var xmlhttp; 

そして、それはページではなく、その機能に適用されます。

何らかの理由でページの範囲を指定する必要がある場合は、その名前を変更して、たとえば次のようにします。 xmlhttpHelloxmlhttpAnotheroneの両方です。

+0

ありがとうございます!それを完全に忘れてしまった。 – Supra