2013-10-26 39 views
0

我有多个按钮的页面,有时可以非常快速地点击 - 但是看起来Chrome可以保持速度?多次点击可以防止AJAX请求

$(document).ready(function() { 
    $('.favorite-button').unbind('click').click(function() { 
    addLine($(this).data('line-id')); 
    }); 
}); 

addLine()函数:

function addLine(lineID) { 
    $.ajax({ 
     type: 'POST', 
     url: 'add-line.php', 
     data: { lineID : lineID }, 
     success: function(server_response) { 
      $('#counter-lines').html(server_response); 
     } 
    }); 
} 

我能做些什么使Chrome将注册和完成每一项Ajax调用? 它似乎并不是Chrome的限制 - 即使两次快速点击都会导致ajax调用失败...

+0

你确定它不是顺序返回的答案是问题?这可能是第二次点击后回来...... –

+0

不知道?尽管如此,这应该不重要。每一次点击都会在数据库中插入一行 - 如果所有的AJAX请求都已完成,我应该可以在10秒后点击一个按钮并显示所有行...但是,所有行都不会插入数据库中,因为它们应该是:(只有当我强迫我的手指冷却点击;)) – EibergDK

+1

你检查铬网络选项卡,你没有得到一个错误吗? –

回答

0

看起来你只是没有得到响应的顺序。

我看到你告诉服务器什么ID给行。那么为什么你不把发送的lineID保存在一个全局变量中,当你得到响应时,你可以看到它是否是对最近请求的响应。

,如:

var latest_line = null; 
    function addLine(lineID) { 
      latest_line = lineID; 
      $.ajax({ 
       type: 'POST', 
       url: 'add-line.php', 
       data: { lineID : lineID }, 
       success: function(server_response) { 
        // Is this the most recent request? 
        if (latest_line == lineID) 
        { 
         $('#counter-lines').html(server_response); 
        } 
       } 
      }); 
     } 
+0

这似乎不是解决OP的问题。此外,您不需要将'lineID'分配给'curr_line'。参数'lineID'对你正在做的事情会很好。这个函数之外没有任何东西可以改变'lineID'参数。 – jfriend00

+0

对不起。我认为lineID是一个全局变量。刚才我看到它是一个参数。 – Yochai

+0

我更新了我的答案。谢谢jfriend00 – Yochai

0

您的代码必须是这样的:

$(document).ready(function(){ 
    js_Codes(); 

    /* The rest of your code that will not reload after ajax request */ 

}); 

$(document).ajaxComplete(function(){ 
    /* Here we call the codes that will reload after your ajax request */ 
    js_Codes(); 
}); 

function js_Codes(){ 

    function clickOpenDialog(){ 
     $('.btOpenDialog').off('click',clickOpenDialog); 
     fn_open_Dialog_Click($(this).prop('alt'), function(){$('.btOpenDialog').on('click',clickOpenDialog);}); 
     return false; 
    }; 

    function fn_Open_Dialog_Click(myString){ 
     var myArray = myString.split(';'); 
     /* whatever u want with your page with a string param array */ 

    }; 

    $('.btOpenDialog').on('click',clickOpenDialog); 
} 

工作正常,我..;)