setTimeOut() 不适用于 AJAX

发布于 2024-08-05 08:48:54 字数 950 浏览 9 评论 0原文

在我的应用程序中,我想在一段时间后向服务器发送一些内容。 我已经使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

隔纱相望 2024-08-12 08:48:55

此代码段中存在几个导致错误的问题:

window.onload = show_data('Dev');

关于为什么 window.onload = show_data(' Dev'); 不起作用可能是这样的:window.onload 需要是一个函数。当您的代码执行时,它会评估 show_data('Dev') (它不返回值,但会启动 XMLHTTPRequest,因此它似乎可以工作)并执行 window.onload = 未定义;

window.onload=show_data; 可以工作 - 但是你不会传递参数。

幸运的是,JavaScript 允许您创建匿名函数 - 导致:

window.onload = function() { show_data('Dev'); };

setTimeOut(show_data('Dev'), 10000);

首先,JavaScript 是一种区分大小写的语言。 setTimeOut !== setTimeout。此外,setTimeout/setInterval 的第一个参数应该是一个函数,它遇到与 window.onload 相同的问题,它调用 setTimeout(undefined, 10000);setTimeout(undefined, 10000); code>,但立即执行请求。我还认为您的意思是使用与调用时相同的 strName 来调用它。

您的超时设置应该说:

setTimeout(function() {show_data(strName);}, 10000); 

旁注 - setInterval()setTimeout() 都允许传递在运行时获取 eval() 的字符串,但我建议不要使用如下所示的方法:

// please dont use me
setTimeout("show_data(strname)", 10000);

此时,使用这两行编辑时,您的代码应该可以工作。剩下的这些东西只是其他优化

setTimeout()setInterval()

这看起来它会继续每 10000ms 检查一次,除非 ajax 请求失败,然后它会停止。我想你只是想永远投票。 setInterval() 允许您设置“重复”计时器。

删除 setTimeout 行并将 window.onload 替换为:

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);

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);
十雾 2024-08-12 08:48:55

替换

window.onload = show_data('Dev');

window.onload = function() {
   var intervalID = setInterval( function(){ show_data('Dev')}, 10000);
}

并从中删除

setTimeOut(show_data('Dev'), 10000);

stateChangeHandler 函数

。并且不要忘记

使用

clearInterval(intervalID);

setIntervalsetTimeout 之间的区别在于,setTimeout() 仅触发表达式一次,而 < code>setInterval() 不断地一次又一次地触发表达式(除非你告诉它停止)。

Replace

window.onload = show_data('Dev');

with

window.onload = function() {
   var intervalID = setInterval( function(){ show_data('Dev')}, 10000);
}

and remove

setTimeOut(show_data('Dev'), 10000);

from stateChangeHandler function.

and don't forget to clear the interval

using

clearInterval(intervalID);

The difference between setInterval and setTimeout is that, setTimeout() triggers expression only once, whereas setInterval() keeps triggering expression again and again (unless you tell it to stop).

往日 2024-08-12 08:48:55

而不是:

setTimeOut(show_data('Dev'), 10000);

你可以/已经尝试过:

setTimeOut(function() { show_data('Dev') }, 10000);

instead of:

setTimeOut(show_data('Dev'), 10000);

could/have you tried:

setTimeOut(function() { show_data('Dev') }, 10000);

?

瀞厅☆埖开 2024-08-12 08:48:55

你的window.onload方法肯定是错误的。将其替换为:

window.onload = function(){var intervalID = setInterval(function(){show_data('Dev')}, 10000);}

这里的其他答案似乎涵盖了其他问题,所以我不会触及它。干杯

Your window.onload method is definitely wrong. replace that with this:

window.onload = function(){var intervalID = setInterval(function(){show_data('Dev')}, 10000);}

Other answers here seem to cover the other problems so I won't touch that. cheers

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文