2014-09-27 62 views
0

我遇到了ajax调用的问题。 我有一些代码设置运行的功能,每2秒,这看起来看到,如果内容已经与WordPress AJAX更新与否,确实有一些用PHP的工作,然后更新数据库:ajax在数据库解锁后多次触发

window.setInterval("updateContent()", 2000); 

function updateContent(){  

    if($('#needcontent').hasClass('yes')){ 
     CONTENT.updateContent('monitor' , ids); 
    } 

} 

$(function() { 

    CONTENT= { 

     updateContent: function(callback, data){ 

      data = {    
       action: 'myplugin_do_ajax', 
       callback: callback, 
       data: data        
      }; 

      $.post(ajaxurl, data, function(response){ 

       switch(data.callback){ 

        case 'monitor' : 

         data_returned = eval("(" + response + ")"); 

         if(data_returned.completed == 'true'){ 
          //Adjust the DOM because there was a content update, and remove the class "yes" from #needcontent to stop the check until next time 
         } 
         else{ 
          //Do nothing because no content was found, let the Interval run again 
         } 

        break; 

       } 


      } 

    } 

} 

问题我发现有时这些内容非常大,并且在php更新数据库时锁定表格。 ajax调用运行一次,运行到数据库锁中,并且不会返回任何内容,直到数据库再次解锁。数据库可能会被锁定10秒,导致1次运行和4次未运行的呼叫。

UPDATE: 这不是数据库锁定,它是PHP函数返回时间超过2秒,导致Interval一次又一次地循环而没有响应。

发生了什么事情是那些4个未运行的ajax调用然后开始一个接一个地发射一个像他们试图赶上或什么的。

我试过将间隔时间增加到10秒,但这并没有解决问题,因为如果数据库被锁定了11秒,它仍然会发生两次。

我已经尝试在JavaScript(yuck)中使用全局变量来停止调用该函数的时间间隔,但这似乎并不奏效。

UPDATE 2: 我在下面回答了我自己的问题,以帮助我解决问题。

+0

不使用'setInterval',使用'AJAX的成功,而不是 – charlietfl 2014-09-27 16:21:16

+0

@charlietfl内setTimeout' - 谢谢。您将我的想法转向解决方案,为我解决这个问题 – David 2014-09-28 02:50:56

回答

0

试试这个:

window.updateCheck= window.setInterval("updateContent()", 2000); 

function updateContent(){  

    if($('#needcontent').hasClass('yes')){ 
     CONTENT.updateContent('monitor' , ids); 
     clearInterval(window.updateCheck); 
    } 

} 

$(function() { 

    CONTENT= { 

     updateContent: function(callback, data){ 

      data = {    
       action: 'myplugin_do_ajax', 
       callback: callback, 
       data: data        
      }; 
      if(window.ajaxCall) window.ajaxCall.abort(); 

      window.ajaxCall= $.post(ajaxurl, data, function(response){ 
       window.updateCheck= window.setInterval("updateContent()", 2000); 
       switch(data.callback){ 

        case 'monitor' : 

         data_returned = eval("(" + response + ")"); 

         if(data_returned.completed == 'true'){ 
          //Adjust the DOM because there was a content update, and remove the class "yes" from #needcontent to stop the check until next time 
         } 
         else{ 
          //Do nothing because no content was found, let the Interval run again 
         } 

        break; 

       } 


      } 

    } 

} 

我们在这里做是把在一个全局变量的时间间隔,并在Ajax调用($。员额()调用实际上),更新那么当条件被检查,我们停止时间间隔,终止所有其他活动或排队的请求并将ajax请求发送到服务器。

当请求被发送到服务器时,更新检查被停止,然后一旦服务器响应请求,我们再次开始更新检查!在我的OP

+0

'尝试这个'很难解释您的解决方案在OP代码或方法中的变化。人们不应该需要使用diff工具来知道差异在哪里 – charlietfl 2014-09-27 16:24:11

+0

@charlietfl我正在写解释!对于延迟 – 2014-09-27 16:25:35

+0

对不起,虽然这是一个很好的答案,并适用于其他情况,但我想尽量避免在全局空间中使用setInterval。主要是因为它会涉及一个重要的代码重写。看到我的答案是如何解决这个问题的。 – David 2014-09-28 02:50:24

0

charlietfl的评论让我思考的setInterval和setTimeout的之间的区别,我意识到:

一)的setInterval会连续运行,不管退回或不结果。在我的情况下,使用ajax,该函数正在异步调用,所以setInterval不会在乎是否返回结果。

b)如果我更改了代码以使用setTimeout,我可以更多地控制结果。

这就是我所做的。

首先,完全删除setInterval函数,这是不需要的。

然后,我改变了我的开关情况如下:

case 'monitor' : 

    var monitor = false; 

    data_returned = eval("(" + response + ")"); 

    if(data_returned.completed == 'true'){ 
     //Adjust the DOM because there was a content update, and remove the class "yes" from #needcontent to stop the check until next time 
    } 
    else{ 
     //Run the monitor after a 2 second timeout. 
     var ids = j('.contentDiv').attr('id'); 
     window.setTimeout(CONTENT.updateContent('monitor' , ids) , 2000); 
    } 

break;