2015-11-19 44 views
3

我有一个Web应用程序,其中有一个“投票”按钮。可以在40秒间隔内点击“投票”按钮的次数。Codeigniter:太多的Ajax调用导致Web应用程序超时

对于每次单击,都会创建一个将数据插入数据库的异步ajax请求。这适用于少数用户。当有300多个用户单击投票按钮时,应用程序无法正确响应,导致超时错误。

请让我知道ant建议绕过这个问题。

请注意,CPU使用率接近50%。

+0

你为什么不按用户操作多少次,只发送一次Ajax请求他点击40秒内的投票按钮? –

+0

感谢您的评论。问题是我们需要实时的结果。我们需要知道每一秒的进展情况。 – Cipher

+4

Ajax并不适合任何实时技术。考虑使用例如websockets –

回答

0

尝试这个代码, 粘贴在你的脚本,比使用ajax_call代替$就

$(function(){ 
    window.queue = $.jqmq({ 
     delay : -1, 
     batch : 2, 
     callback:function(options){ 
      $.each(options,function(i,option){ 
       $.ajax({ 
        url   : option.url, 
        type  : option.type, 
        data  : option.data, 
        dataType : option.dataType, 
        beforeSend : function(){ 
         option.beforeSend(); 
        }, 
        success  : function(result){ 
         option.success(result); 
        }, 
        error  : function(xhr,status, error){ 
         option.error(xhr,status, error); 
        }, 
        complete : function(){ 
         queue.next(false); 
        } 
       }); 
      }); 
     }, 
     complete:function() { 
      console.log("done"); 
     } 
    }); 
}); 
var ajax_call = function(data){ 
    var _defaults = { 
     url   : "", 
     data  : {}, 
     type  : "post", 
     dataType : "", 
     beforeSend : _default_beforeSend, 
     success  : _default_success, 
     error  : _default_error 
    } 

    var options = $.extend(_defaults,data); 

    if(options.url != ''){ 
     queue.add(options); 
    } 
    else{ 
     alert("Url is not defined in ajax_call"); 
    } 
} 
var _default_success = function(){ 
    alert("Please define 'success' funciton in ajax_call for remove this alert!"); 
} 
var _default_beforeSend = function(){ 
    alert("Please define 'beforeSend' funciton in ajax_call for remove this alert!"); 
} 
var _default_error = function(){ 
    alert("Please define 'error' funciton in ajax_call for remove this alert!"); 
} 
相关问题