2014-02-20 45 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