setTimeOut() 不适用于 AJAX
在我的应用程序中,我想在一段时间后向服务器发送一些内容。 我已经使用 AJAX 实现了它。但它是第一次工作但不是递归地执行。我已经使用 setTimeOut() 来做到这一点。
var xmlHttp;
var requestURL = 'http://localhost:1092/ClassicAJAXDemo/UpdateHeartbeat.aspx?name=';
function show_data(strName)
{
if (strName.length > 0)
{
var url = requestURL + strName;
xmlHttp = GetXmlHttpObject(stateChangeHandler);
xmlHttp_Get(xmlHttp, url);
}
}
function stateChangeHandler()
{
if (xmlHttp.readyState == 4)
{
var str = xmlHttp.responseText;
setTimeOut(show_data('Dev'), 10000); // It is not waiting for 10 seconds.
}
}
function xmlHttp_Get(xmlhttp, url)
{
xmlhttp.open('GET', url, true);
xmlhttp.send(null);
}
function GetXmlHttpObject(handler)
{
return new XMLHttpRequest();
}
window.onload = show_data('Dev');
In my application i want to send something to the server after some time.
I have implemented it using AJAX. But it works for the first time but not doing it recursively. I have used setTimeOut() for doing that.
var xmlHttp;
var requestURL = 'http://localhost:1092/ClassicAJAXDemo/UpdateHeartbeat.aspx?name=';
function show_data(strName)
{
if (strName.length > 0)
{
var url = requestURL + strName;
xmlHttp = GetXmlHttpObject(stateChangeHandler);
xmlHttp_Get(xmlHttp, url);
}
}
function stateChangeHandler()
{
if (xmlHttp.readyState == 4)
{
var str = xmlHttp.responseText;
setTimeOut(show_data('Dev'), 10000); // It is not waiting for 10 seconds.
}
}
function xmlHttp_Get(xmlhttp, url)
{
xmlhttp.open('GET', url, true);
xmlhttp.send(null);
}
function GetXmlHttpObject(handler)
{
return new XMLHttpRequest();
}
window.onload = show_data('Dev');
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
此代码段中存在几个导致错误的问题:
window.onload = show_data('Dev');
关于为什么
window.onload = show_data(' Dev');
不起作用可能是这样的:window.onload
需要是一个函数。当您的代码执行时,它会评估show_data('Dev')
(它不返回值,但会启动 XMLHTTPRequest,因此它似乎可以工作)并执行window.onload = 未定义;
。window.onload=show_data;
可以工作 - 但是你不会传递参数。幸运的是,JavaScript 允许您创建匿名函数 - 导致:
setTimeOut(show_data('Dev'), 10000);
首先,JavaScript 是一种区分大小写的语言。
setTimeOut !== setTimeout
。此外,setTimeout/setInterval 的第一个参数应该是一个函数,它遇到与 window.onload 相同的问题,它调用 setTimeout(undefined, 10000);setTimeout(undefined, 10000); code>,但立即执行请求。我还认为您的意思是使用与调用时相同的strName
来调用它。您的超时设置应该说:
旁注 -
setInterval()
和setTimeout()
都允许传递在运行时获取eval()
的字符串,但我建议不要使用如下所示的方法:此时,使用这两行编辑时,您的代码应该可以工作。剩下的这些东西只是其他优化
setTimeout()
与setInterval()
这看起来它会继续每 10000ms 检查一次,除非 ajax 请求失败,然后它会停止。我想你只是想永远投票。
setInterval()
允许您设置“重复”计时器。删除
setTimeout
行并将window.onload
替换为:You have a couple of issues in this code segment that are creating your bugs:
window.onload = show_data('Dev');
A bit of explanation about why
window.onload = show_data('Dev');
doesn't work is probably in order:window.onload
needs to be a function. As your code executes, it is evaluatingshow_data('Dev')
(which doen't return a value, but does start an XMLHTTPRequest, so it appears to work) and executingwindow.onload = undefined;
.window.onload=show_data;
would work - but then you don't get your parameter passed.Luckily JavaScript allows you to create anonymous functions - leading to:
setTimeOut(show_data('Dev'), 10000);
First of all, JavaScript is a case sensitive language.
setTimeOut !== setTimeout
. Also the first parameter to setTimeout/setInterval is expected to be a function, which suffers from the same problem here as yourwindow.onload
did, it callssetTimeout(undefined, 10000);
, but executes the request immediately. I also think you mean to call it with the samestrName
as it was called with.Your timeout set should say:
A side note -
setInterval()
andsetTimeout()
both allow passing strings that geteval()
ed at runtime, but I would suggest against using that method which looks like this:At this point your code should work when edited with those two lines. The rest of this stuff is just other optimizations
setTimeout()
vs.setInterval()
This seems like it is going to keep checking every 10000ms, unless the ajax request fails, then it will stop. I imagine you just wanted to poll forever.
setInterval()
allows you to setup a "repeating" timer.Remove your
setTimeout
line and replacewindow.onload
with:替换
为
并从中删除
stateChangeHandler
函数。并且不要忘记
使用
setInterval
和setTimeout
之间的区别在于,setTimeout()
仅触发表达式一次,而 < code>setInterval() 不断地一次又一次地触发表达式(除非你告诉它停止)。Replace
with
and remove
from
stateChangeHandler
function.and don't forget to clear the interval
using
The difference between
setInterval
andsetTimeout
is that,setTimeout()
triggers expression only once, whereassetInterval()
keeps triggering expression again and again (unless you tell it to stop).而不是:
你可以/已经尝试过:
?
instead of:
could/have you tried:
?
你的window.onload方法肯定是错误的。将其替换为:
这里的其他答案似乎涵盖了其他问题,所以我不会触及它。干杯
Your window.onload method is definitely wrong. replace that with this:
Other answers here seem to cover the other problems so I won't touch that. cheers