在 JavaScript 中每 10 秒发送一次 xmlHttpRequest
我运行一个 JavaScript 函数,将 xmlHttpRequest
发送到 .ashx
(我们将其命名为 send_req()
,首次在页面加载时运行) 。对于 onreadystatechange
,我有一个函数可以接收 XML 数据并将其显示在页面上(我们将其命名为 getanswer()
)。
我想每 20 秒自动更新一次页面上的 XML 数据。为此,我在 writexml()
末尾使用 setTimeout(send_req(),20000)
,但它不会更新页面上的数据。我在代码中的 ****
行添加了 alert()
。它每秒显示在页面上!
如果我在没有 setTimeout
的情况下使用它,我的代码可以正常工作。
这是我的代码
var Population = "";
var Available_money = "";
var resource_timer;
var httpReq_resource;
function send_req() {
if (window.ActiveXObject) {
httpReq_resource = new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest) {
httpReq_resource = new XMLHttpRequest();
}
var sendStr = "user_id=1";
if (httpReq_resource)
{
httpReq_resource.onreadystatechange = getanswer;
httpReq_resource.open("POST", "Answer_Resource_change.ashx");
httpReq_resource.send(sendStr);
}
}
function getanswer() {
var results = httpReq_resource.responseXML;
if (httpReq_resource.readyState == 4) {
if (httpReq_resource.status == 200) {
try {
var value;
var values = results.getElementsByTagName("values");
for (var i = 0; i < values.length; i++) {
value = values[i];
Population = value.getElementsByTagName("Population")[0].firstChild.nodeValue;
Available_money = value.getElementsByTagName("Available_money")[0].firstChild.nodeValue;
... and some more like two line up
}
make_changes();
**********************************
resource_timer = setTimeout(send_req(), 20000);
}
catch (e) {
}
}
}
}
function make_changes() {
$("li span#l1").text(Available_money + '/' + Population);
...and some more like up line
}
I run a JavaScript function that send a xmlHttpRequest
to an .ashx
(let's name it send_req()
that run on page load for first time). For onreadystatechange
, I have a function that receive XML data and show it on the page (let's name this one getanswer()
).
I want to automatically update XML data on the page every 20 seconds. For that, I use setTimeout(send_req(),20000)
in the end of writexml()
, but it doesn't update data on the page. I add an alert()
at the ****
line in the code. It shows on the page every one second!
And my code works fine if I use it without setTimeout
.
Here is my code
var Population = "";
var Available_money = "";
var resource_timer;
var httpReq_resource;
function send_req() {
if (window.ActiveXObject) {
httpReq_resource = new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest) {
httpReq_resource = new XMLHttpRequest();
}
var sendStr = "user_id=1";
if (httpReq_resource)
{
httpReq_resource.onreadystatechange = getanswer;
httpReq_resource.open("POST", "Answer_Resource_change.ashx");
httpReq_resource.send(sendStr);
}
}
function getanswer() {
var results = httpReq_resource.responseXML;
if (httpReq_resource.readyState == 4) {
if (httpReq_resource.status == 200) {
try {
var value;
var values = results.getElementsByTagName("values");
for (var i = 0; i < values.length; i++) {
value = values[i];
Population = value.getElementsByTagName("Population")[0].firstChild.nodeValue;
Available_money = value.getElementsByTagName("Available_money")[0].firstChild.nodeValue;
... and some more like two line up
}
make_changes();
**********************************
resource_timer = setTimeout(send_req(), 20000);
}
catch (e) {
}
}
}
}
function make_changes() {
$("li span#l1").text(Available_money + '/' + Population);
...and some more like up line
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
应该是
:
第一个在 20 秒后执行
send_req()
的结果,第二个执行send_req
本身。This:
Should be:
The first executes the result of
send_req()
after 20 seconds, the second executessend_req
itself.