使用的setTimeout与Ajax调用 [英] using SetTimeout with Ajax calls

查看:743
本文介绍了使用的setTimeout与Ajax调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用的setTimeout来检查,如果表中的数据存在:

如果存在数据不获取数据。如果数据DES不存在使用负载获取数据,然后做同样的事情每隔x分钟。

下面是我迄今。出于某种原因,的setTimeout 当它击中如果块不工作。

我甚至不知道这是做到这一点的最好办法。

  VAR sTimeOut = setTimeout的(函数(){
        $阿贾克斯({
            网址:'CheckIfDataExists /+
                         新的Date()。的getTime()
            成功:函数(响应){
                如果(响应=='真'){
                    $('。DataDiv')
                      .load('GetFreshData /+新的Date()
                          .getTime(){ID:$(#的RowID)VAL()});
                }
            },
            完成:功能(){
                clearTimeout(sTimeOut);
            }
        });
    },10000);

任何帮助将大大AP preciated。

更新...

 的setTimeout(函数(){checkData()},5000);
    功能checkData(){
    $阿贾克斯({
            网址:'CheckIfDataExists /+
                             新的Date()。的getTime()
            成功:函数(响应){
                如果(响应=='真'){
                    $('。DataDiv')
                          .load('GetFreshData /+新的Date()
                              .getTime(){ID:$(#的RowID)VAL()});
                }其他{
                    $('OutOfWindow。')HTML(没有找到数据)。
                    的setTimeout(函数(){checkData()},5000);
                }
            },
            完成:功能(){
               // clearTimeout(sTimeOut);
            }
        });
    }


解决方案

这样的事情应该工作,第一段本地化,所以我可以试运行。我已经解释了code和它下面是您code应

就像你实现(从您的文章您的更新)的setTimeout 只要求你的目标函数的一次,那么继续检查你需要调用再次,如果你做一个检查失败。

请参阅它的jsfiddle: http://jsfiddle.net/jQxbK/

  //我们存储了timerIdhere
变种timeOutId = 0;
//我们定义的功能,并在VAR店呢
VAR ajaxFn =功能(){
        $阿贾克斯({
            网址:'/回声/ HTML /',
            成功:函数(响应){
                如果(响应=='真'){// YAYA
                    clearTimeout(timeOutId); //停止超时
                }其他{//失败检查?
                    timeOutId = setTimeout的(ajaxFn,10000); //重新设置超时
                    的console.log(呼); //检查是否这是运行
                    //你应该看到这对的jsfiddle
                    //由于响应也只是一个空字符串
                }
            }
        });
}
ajaxFn(); //我们调用我们的存储功能
//或者你想等待你的第一个电话之前10秒?
//使用这行代替
timeOutId = setTimeout的(ajaxFn,10000);


您code应该是这样的:

  VAR timeOutId = 0;
VAR ajaxFn =功能(){
        $阿贾克斯({
            网址:'CheckIfDataExists /+新的Date()的getTime()
            成功:函数(响应){
                如果(响应=='真'){
                    $('。DataDiv')。
                    负载('GetFreshData /+新的Date()。
                            的getTime(){ID:$(#的RowID)VAL()});
                     clearTimeout(timeOutId);
                }其他{
                    timeOutId = setTimeout的(ajaxFn,10000);
                    的console.log(呼);
                }
            }
            });
}
ajaxFn();
//或者使用下面排队等待第一个电话之前10秒
timeOutId = setTimeout的(ajaxFn,10000);

I am trying to use setTimeout to check if data exists in a table:

If the data exists don't fetch data. If the data des not exist fetch the data using load and then do the same thing every x minutes.

Here is what I have so far. For some reason, the setTimeout does not work when it hits the If block.

I am not even sure if this is the best way to do this.

    var sTimeOut = setTimeout(function () {
        $.ajax({
            url: 'CheckIfDataExists/' +
                         new Date().getTime(),
            success: function (response) {
                if (response == 'True') {
                    $('.DataDiv')
                      .load('GetFreshData/' + new Date()
                          .getTime(), { "Id": $("#RowID").val() });
                }
            },
            complete: function () {
                clearTimeout(sTimeOut);
            }
        });
    }, 10000);

Any help will be greatly appreciated.

Updated ...

    setTimeout(function(){checkData()}, 5000);
    function checkData(){
    $.ajax({ 
            url: 'CheckIfDataExists/' + 
                             new Date().getTime(),
            success: function (response) {
                if (response == 'True') {                     
                    $('.DataDiv')
                          .load('GetFreshData/' + new Date()
                              .getTime(), { "Id": $("#RowID").val() });
                } else {
                    $('.OutOfWindow').html('No Data Found');
                    setTimeout(function () { checkData() }, 5000);
                }
            }, 
            complete: function () { 
               // clearTimeout(sTimeOut); 
            } 
        }); 
    }

解决方案

Something like this should work, the first snippet is localized so I could test run it. I've explained the code and below it is what your code should be

Like you realized (from your update on your post) setTimeout only calls your target function once, so to keep checking you need to call it again if you do a check that fails.

See it on JsFiddle : http://jsfiddle.net/jQxbK/

//we store out timerIdhere
var timeOutId = 0;
//we define our function and STORE it in a var
var ajaxFn = function () {
        $.ajax({
            url: '/echo/html/',
            success: function (response) {
                if (response == 'True') {//YAYA
                    clearTimeout(timeOutId);//stop the timeout
                } else {//Fail check?
                    timeOutId = setTimeout(ajaxFn, 10000);//set the timeout again
                    console.log("call");//check if this is running
                    //you should see this on jsfiddle
                    // since the response there is just an empty string
                }
            }
        });
}
ajaxFn();//we CALL the function we stored 
//or you wanna wait 10 secs before your first call? 
//use THIS line instead
timeOutId = setTimeout(ajaxFn, 10000);


Your code should look like this :

var timeOutId = 0;
var ajaxFn = function () {
        $.ajax({
            url: 'CheckIfDataExists/' + new Date().getTime(),
            success: function (response) {
                if (response == 'True') {
                    $('.DataDiv').
                    load('GetFreshData/' + new Date().
                            getTime(), { "Id": $("#RowID").val() });
                     clearTimeout(timeOutId);
                } else {
                    timeOutId = setTimeout(ajaxFn, 10000);
                    console.log("call");
                }
            }
            });
}
ajaxFn();
//OR use BELOW line to wait 10 secs before first call
timeOutId = setTimeout(ajaxFn, 10000);

这篇关于使用的setTimeout与Ajax调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆