【问题标题】:Codeigniter: too many ajax calls is causing web application timeoutCodeigniter:太多的 ajax 调用导致 Web 应用程序超时
【发布时间】:2016-02-22 00:22:13
【问题描述】:

我有一个 Web 应用程序,其中有一个“投票”按钮。 “投票”按钮可以在 40 秒的间隔内按用户可以点击的次数。

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

请告诉我 ant 绕过问题的建议。

请注意,CPU 使用率几乎是 50%。

【问题讨论】:

  • 为什么不为每个用户只发送一次 ajax 请求来处理他在 40 秒内点击了多少次投票按钮?
  • 感谢您的评论。问题是我们需要实时的结果。我们需要知道每一秒的进度。
  • Ajax 并不适合任何实时技术。考虑使用例如 websockets
  • 也许我会试一试。你知道用 php 或 codeigniter 实现 websockets 的好库吗?
  • 试试这个:socketo.me

标签: php jquery ajax codeigniter timeout


【解决方案1】:

试试这个代码, 将此粘贴到您的脚本中,然后使用 ajax_call 而不是 $.ajax

$(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!");
}

【讨论】:

    猜你喜欢
    • 2017-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-14
    • 1970-01-01
    • 2018-05-11
    • 1970-01-01
    • 2017-01-03
    相关资源
    最近更新 更多