如何使用 setInterval 从 ajax 页面本身重复 ajax 调用?
我已将其保存为 ajax.js 文件:
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();
}
基本上,我尝试使用内置的 setInterval() 函数来重复 URL 变量包含的任何内容。
从某种意义上说,我需要在调用 tfunction sendAjax(type,str) 后每 5 秒执行一次(如书面形式):
case 'add':
xmlhttp.open("GET","mysql_process_event.php?q="+str,true);
break;
}
xmlhttp.send();
我可以设置在某个时间间隔内编写函数的位置:IE
setInterval( "sendAjax('message', '123')", 5000 );
sendAjax('message','123')
但我有几个、几个地方在我的整个代码中,此函数是写入的,如果它包含在关键操作和 if 语句中,则该函数将不起作用,因为它只会执行一次:
$('#searchbar').live('keyup',function() {
eInput = $(this).val();
if (eInput.length > 1) {
setInterval( "sendAjax('message', '123')", 5000 );
sendAjax('message','123')
}
});
//此函数不起作用。
如果有人可以帮助我修复最后一个函数,或者只是将 setInterval 包含在 Ajax.js 文件本身中,我将不胜感激。
问候, 泰勒
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 jQuery 更轻松地重写
function sendAjax(type, str)
(因为这就是您正在使用的:然后执行以下操作:
You can rewrite that
function sendAjax(type, str)
much easier with jQuery (since thats what it seems you are using:Then do: