2012-02-19 62 views
9

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

如果存在数据不获取数据。如果数据不存在,则使用load读取数据,然后每x分钟执行一次相同的操作。

这是我到目前为止所。由于某些原因,setTimeout在遇到If块时不起作用。

我甚至不确定这是否是最好的方法。

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

任何帮助将不胜感激。

更新...

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); 
      } 
     }); 
    } 
+0

你[检查你的调试器(http://blog.niftysnippets.org /2011/03/no-excuse.html)看看有什么不对吗? JS看起来非常好:http://jsfiddle.net/FgT22/ – gideon 2012-02-19 16:19:41

+0

是的。它工作正常。当数据库中没有数据时(即响应为真时),它按预期工作。如果它是错误的,我想在x分钟后重新检查。这就是问题所在。它不重新检查。我清除成功超时的ida是这样的,下次检查不应该在前一次加载完成之前被触发 – mkizito76 2012-02-19 16:24:12

+0

这就是我现在所看到的,它似乎工作。这是做这件事的最好方法吗?顺便说一句,谢谢你的帮助 – mkizito76 2012-02-19 16:40:19

回答

16

像这样的事情应该工作,第一个片断本地化,所以我可以试运行。我已经解释的代码和它下面是什么你的代码应该是

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

见它的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); 

您的代码应该是这样的:

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

哇,太棒了!非常感谢你的帮助。还有一件事,当负载事件马上触发ajaxFn罚款。我想在ajaxFn触发之前等待(比如10秒)(因为加载可能需要一段时间)。换句话说,我想等10秒钟后再开启ajaxFn。那可能吗? – mkizito76 2012-02-19 17:00:04

+0

@ user1219415你的意思是你想在FIRST电话之前等待10秒?我更新了答案。 – gideon 2012-02-19 17:05:20

+0

很棒!再次感谢 – mkizito76 2012-02-19 17:10:33