2015-11-19 85 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!"); 
} 
相關問題