質問

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

正しい解決策はありません

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top