问题描述
在我的应用程序,我想经过一段时间送东西到服务器。我已经使用AJAX来实现它。但是,它的工作原理是第一次,但没有这样做递归。我已经使用的setTimeout()来这样做。
VAR XMLHTTP;
VAR requestURL ='的http://本地主机:1092 / ClassicAJAXDemo / UpdateHeartbeat.aspx名=';
功能show_data(则strName)
{
如果(strName.length大于0)
{
VAR URL = requestURL +则strName;
XMLHTTP = GetXmlHttpObject(stateChangeHandler);
xmlHttp_Get(XMLHTTP,URL);
}
}
功能stateChangeHandler()
{
如果(xmlHttp.readyState == 4)
{
VAR海峡= xmlHttp.responseText;
的setTimeout(show_data(开发),10000); //这是不是在等待10秒。
}
}
功能xmlHttp_Get(XMLHTTP,网址)
{
xmlhttp.open(GET,URL,真正的);
xmlhttp.send(空);
}
功能GetXmlHttpObject(句柄)
{
返回新XMLHtt prequest();
}
在window.onload = show_data('开发');
您必须在创建你的错误该code段中的几个问题:
的window.onload = show_data('开发');
解释为什么的window.onload = show_data('开发')的位;
不工作,可能是为了:窗口。 onload事件
需要一个功能。当你的code执行,它正在评估 show_data('开发')
(其中doen't返回一个值,但会启动一个XMLHTT prequest,所以它似乎工作)和执行的window.onload =不确定;
的window.onload = show_data;
将工作 - 但你没有得到你的参数传递
幸运的是,JavaScript允许你创建匿名函数 - 导致:
的window.onload =功能(){show_data('开发'); };
的setTimeout(show_data(开发),10000);
首先,JavaScript是区分大小写语言。 的setTimeout!==的setTimeout
。另外,第一个参数的setTimeout / setInterval的预期是一个函数,它从同一个问题在这里受到作为你的的window.onload
一样,它会调用的setTimeout(未定义,10000);
,但立即执行的请求。我也觉得你的意思是用相同的则strName
,因为它被调用。
您超时设置应该说:
的setTimeout(函数(){show_data(则strName);},10000);
一个侧面说明 - 的setInterval()
和的setTimeout()
都允许通过字符串获得的eval()
版在运行,但我建议不要使用该方法,看起来是这样的:
//请不要利用我
的setTimeout(show_data(则strName),10000);
在这一点上,当与这两个行编辑您的code应该工作。这东西剩下的只是其他优化
的setTimeout()
与的setInterval()
这好像它是要继续检查每个10000ms,除非AJAX请求失败,那么它就会停止。我想你只是想轮询永远。 的setInterval()
允许您设置一个重复计时器。
删除你的的setTimeout
行并替换的window.onload
与
VAR updateInterval;
在window.onload =功能(){
updateInterval =的setInterval(函数(){show_data('开发');},10000);
};
//一个例子 - 让从现在开始停止轮询35秒
的setTimeout(函数(){
clearInterval(updateInterval);
},35000);
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');
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 evaluating show_data('Dev')
(which doen't return a value, but does start an XMLHTTPRequest, so it appears to work) and executing window.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:
window.onload = function() { show_data('Dev'); };
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 your window.onload
did, it calls setTimeout(undefined, 10000);
, but executes the request immediately. I also think you mean to call it with the same strName
as it was called with.
Your timeout set should say:
setTimeout(function() {show_data(strName);}, 10000);
A side note - setInterval()
and setTimeout()
both allow passing strings that get eval()
ed at runtime, but I would suggest against using that method which looks like this:
// please dont use me
setTimeout("show_data(strname)", 10000);
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 replace window.onload
with:
var updateInterval;
window.onload = function(){
updateInterval = setInterval(function(){ show_data('Dev'); }, 10000);
};
// an example - lets stop polling 35 seconds from now
setTimeout(function() {
clearInterval(updateInterval);
}, 35000);
这篇关于的setTimeout()不工作与AJAX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!