开发者

How to use setInterval to repeat ajax calls from the ajax page itself?

开发者 https://www.devze.com 2023-03-04 23:56 出处:网络
I have saved this as an ajax.js file: function sendAjax(type, str) { var xmlhttp; if (str==\"\") { document.getElementById(\"txtResp\").innerHTML=\"\";

I have saved this as an ajax.js file:

function sendAjax(type, str)
{
var xmlhttp;
if (str=="")
  {
  document.getElementById("txtResp").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {
  xmlhttp=new XMLHttpRequest();
  }
else
  {
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtResp").innerHTML=xmlhttp.responseText;
    }
  }


switch(type)
{
case 'search':
    xmlhttp.open('GET','mysql_process_search.php?q='+str,true); 
  break;
case 'add':
    xmlhttp.open("GET","mysql_process_event.php?q="+str,true); 
  break;
}

xmlhttp.send();
}

Basically, I am trying to use the built in setInterval() function to repeat whatever the URL variables contain.

In a sense, I need this to execute every 5 seconds after he tfunction sendAjax(type,str) is called (as in writ):

case 'add':
    xmlhttp.open("GET","mysql_process_event.php?q="+str,true); 
  break;
}

xmlhttp.send();

I could just set where I write the function on an interval: I.E.

setInterval( "sendAjax('message', '123')", 5000 );
sendAjax('message','123')

But I have several, several places throughout my code where this function is writ and this wont work if it is contained within an key action and if statement because it will only execute once:

$('#searchbar').live('keyup',function() {
    eInput = $(this).val();
    if (eInput.length > 1) {
    setInterval( "sendAjax('message', '123')", 5000 )开发者_运维知识库;
    sendAjax('message','123')
}
});

//This function doesn't work.

If someone can help me fix the last function, or just to include the setInterval within the Ajax.js file itself, I would greatly appreciate it.

Regards, Taylor


You can rewrite that function sendAjax(type, str) much easier with jQuery (since thats what it seems you are using:

function sendAjax(type, str, succ){  //where succ is the success callback

   if (str=="")
   {
        $("#txtResp").empty();
        return;
   }

   if(succ === undefined) {   succ = null;  }

   switch(type)
   {
   case 'search':
       $.ajax({type:"GET",
               url: "mysql_process_search.php",
               data: "q="+str,
               success: succ
       }); 
     break;
   case 'add':
       $.ajax({type:"GET",
               url: "mysql_process_event.php",
               data: "q="+str,
               success: succ
       }); 
     break;
   }

}

Then do:

$('#searchbar').live('keyup',function() {
    eInput = $(this).val();
    if (eInput.length > 1) {
       sendAjax('message','123', function(responseText){
             $("#txtResp").html(responseText);
             setInterval( function(){
                     sendAjax('message', '123', function(responseText){
                            $("#txtResp").html(responseText);
                     })
              }, 5000 );
       })
    }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号