2014-02-20 46 views
0

一個WebService和往常一樣,原諒我的「泰山」請用英語:P在調用的setInterval

的問題是: 我要打電話與時間間隔的web服務,以確保聯繫人列表上傳完成。

程序是:
1.點擊按鈕發送CSV文件。
2.上傳動畫的div必須能夠在將CSV上傳到數據庫時看到。
3.當websService結束輸入數據時,帶卸載動畫的div必須消失。

我創建了一個JS函數,「我想」是好的,如圖所示:

function loadWS(idArchive){ 
    $('#loaderX').css('display', 'block');   //Here starts with the animation 
    var interrupt=setInterval(function(){    //Start asking the WS 
    $.ajax({ 
     data:{ 
      'idArchive': idArchive, 
     }, 
     url:'/NewsLetters.checkFinal',    //WebService call: the webservice checks 
     type:'post',         //if register entry is complete. 
     success:function(res){ 
      var r=eval(res); 
      if(r==1){         //IF Response ok? we've finished the task 
       clearInterval(interrupt); 
       load_sec(link,106);     //This reloads the section via AJAX 
       $('#loaderX').css('display', 'none'); //Here stops the animation 
      } 
      if (r==0) { 
      } 
     } 
    }); 
},1000); 
} 

難道我做錯了什麼?在setInterval過程中調用WS是否正確?

感謝所有提前!

+0

嘗試一次,然後如果您有任何問題,請告訴我們。 –

+0

它可以正常工作,如果我只稱它一次。我認爲問題是遞歸或其他...響應也是正確的... – aNGRoN

+0

1秒是相當少我猜。嘗試增加間隔。 –

回答

1

只做下面的事情,並增加至少5秒的時間。

setInterval(function(){ 
    $.ajax({ 
     data:{ 
      'idArchive': idArchive, 
     }, 
     url:'/NewsLetters.checkFinal',    //WebService call: the webservice checks 
     type:'post',         //if register entry is complete. 
     success:function(res){ 
      var r=eval(res); 
      if(r==1){         //IF Response ok? we've finished the task 
       clearInterval(interrupt); 
       load_sec(link,106);     //This reloads the section via AJAX 
       $('#loaderX').css('display', 'none'); //Here stops the animation 
      } 
      if (r==0) { 
      } 
     } 
    }); 
}, 5000); 
+0

但沒有什麼不同 – Charles380

1

根據檢查過程你不需要setInterval你只需要再次調用該函數。我會直接做直接的ajax調用。只是要小心,因爲它可能會導致無限循環

function loadWS(idArchive) { 
    $('#loaderX').css('display', 'block'); //Here starts with the animation 
    checkStatus(idArchive); 
} 

function checkStatus(idArchive) { 
    $.ajax({ 
     data: { 
      'idArchive': idArchive 
     }, 
     url: '/NewsLetters.checkFinal', //WebService call: the webservice checks 
     type: 'post', //if register entry is complete. 
     error: function (xhr, status, error) { 
      // do something with the error 
     } 
     success: function (res) { 
      var r = parseInt(res); 
      if (r === 1) { //IF Response ok? we've finished the task     
       load_sec(link, 106); //This reloads the section via AJAX 
       $('#loaderX').css('display', 'none'); //Here stops the animation 
      } else if (r === 0) { 
       setTimeout(checkStatus(idArchive), 1000); 
      } 
     } 
    }); 
} 
+0

這正是我要做的。如果你真的想在ajax調用之間延遲:'if(r == 0){setTimeout(function(){checkStatus(idArchive);},1000); ''。另外,你可能不想使用'eval':http://stackoverflow.com/questions/86513/why-is-using-the-javascript-eval-function-a-bad-idea –

+0

heh我是這麼做的,就像你發表評論 – Charles380

+0

好點我在想parseInt會在這裏工作 – Charles380