JQuery定期获取外部数据而无需重新加载页面

时间:2013-12-13 03:18:32

标签: javascript ajax jquery

您好我想查看运行sql查询的外部文件,看看状态值是否已更改为1.如果是,则重定向到另一个页面。

我使用GET拨打电话,这很好用。

然而,当我使用setTimeout每隔2秒检查它时,它似乎没有运行一次。

我已经尝试了几个小时,我只是无法理解它,请帮忙!

(function loop(){
        $.get("statusPage.jsp", function(data){
        var loadedData = data;
        if(data == 1){
            window.location.replace("http://www.google.com");
        }
    });
    setTimeout(loop,2000)
})();

4 个答案:

答案 0 :(得分:1)

尝试用setInterval

替换setTimeout

答案 1 :(得分:0)

            setInterval(function() {
                //Use setInterval than setTimeout
           $.get("statusPage.jsp", function(data){
               var loadedData = data;
               if(data == 1){
        window.location.replace("http://www.google.com");
               }
                });
                   }, 2000);
               });

使用setInterVal而不是setTimeout。

答案 2 :(得分:0)

它们是函数 setInterval setTimeout 之间的区别 * 解释 *:

<强>的setInterval()

The setInterval() method calls a function or evaluates an expression at specified      intervals (in milliseconds).

 e.g http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_win_setinterval

- setTimeout()

  The setTimeout() method calls a function or evaluates an expression after a specified number of milliseconds.

   e.g. http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_win_settimeout

答案 3 :(得分:0)

好的,我已经成功解决了这个问题。

对于Javascipts来说,我是一个完全的菜鸟。我以为我可以用我的Java知识来解决它。但是我觉得它要困难得多!仍然不确定为什么它会这样运作...

function loop() {
    $.get("statusPage.jsp", function(data){
        if(data == 1){
            window.location.replace("http://www.google.com");
        }
    });
}
var interval = setInterval(loop, 2000);
相关问题