如何结合ajax div

时间:2015-07-19 07:23:27

标签: javascript jquery ajax

这是我的Ajax脚本......我想把第一个和第二个结合起来......

第1次

  <script>
    function Ajax()
    {
    var xmlhttp;
    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("myDiv").innerHTML=xmlhttp.responseText;
        setTimeout('Ajax()',3000);
        }
      }
    xmlhttp.open("GET","Home.php",true);
    xmlhttp.send();
    }

    window.onload=function(){
         setTimeout('Ajax()',3000);
       }
    </script>

第二

<script>
    function Ajax()
    {
    var xmlhttp;
    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("myDiv2").innerHTML=xmlhttp.responseText;
        setTimeout('Ajax()',3000);
        }
      }
    xmlhttp.open("GET","Home2.php",true);
    xmlhttp.send();
    }

    window.onload=function(){
         setTimeout('Ajax()',3000);
       }
    </script>
<body>

<div id="myDiv"></div>
<div id="myDiv2"></div>

</body>

2 个答案:

答案 0 :(得分:1)

假设你想使用jQuery(因为你用它标记了你的问题),这将是你的答案:

function jQueryGet1(){
    $.get('Home.php', function(data){
        $('#myDiv').html(data);
        setTimeout(jQueryGet1, 3000);
    });
}
function jQueryGet2(){
    $.get('Home2.php', function(data){
        $('#myDiv2').html(data);
        setTimeout(jQueryGet2, 3000);
    });
}
$(window).load(function(){
    setTimeout(jQueryGet1, 3000);
    setTimeout(jQueryGet2, 3000);
});

注意:由于您的Ajax调用是针对2个不同的位置,因此无法将这两个函数组合在一起。每个必须单独实施。

答案 1 :(得分:0)

您需要setInterval每3秒刷新一次。函数Ajax只能声明一次。

//Create a function to pass URL to call
//and  a function which will execute on sucess
function Ajax(url, callback) {
    var xmlhttp;
    // code for IE7+, Firefox, Chrome, Opera, Safari
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    } else {
        // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            callback(xmlhttp.responseText);
        }
    }
    xmlhttp.open("GET", url, true);
    xmlhttp.send();
}
window.onload = function() {
    setInterval(function() {
        //Call first url and set response text of div 1
        Ajax("Home.php", function(responseText) {
            document.getElementById("myDiv").innerHTML = responseText;
        })
        //Call other url and set response text of div 2
        Ajax("Home2.php", function(responseText) {
            document.getElementById("myDiv2").innerHTML = responseText;
        })
    }, 3000);
}

标记为时,可以使用

$(function() {
    setInterval(function() {
        $('#myDiv').load('Home.php');
        $('#myDiv2').load('Home2.php');
    }, 3000);
});