2012-02-19 66 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