dongying9756 2016-08-22 13:06
浏览 40

当用户多次单击按钮时,将AJAX请求的数量限制为一个

I have a portal having n tabs and whenever the visitor clicks a particular tab, an AJAX request is created. A PHP script at the back end inserts a record into a table which tracks the time of click and the name of the tab clicked.

When the visitor clicks the same tab multiple times, continuously, all the AJAX requests are succeeding. Say I click a tab "Automation" 5 times, then 5 AJAX requests are succeeding and 5 records are getting inserted. I need to limit the request to only one, if the visitor is repeatedly and quickly clicking the tab for an interval of 3s. How to go about this?

Here's my jQuery AJAX code :

$.ajax({            
    url     :   "pushTelemetry.php",
    cache   :   false,
    data    :   ({
                    DshBrdName  : strFullDashBoardName,
                    DshBrdID    : currentDashboard, 
                    r           : Math.random()
                }),
    success :   function(data, textStatus, jQxhr){
                    //alert(textStatus); 
                },
    error   :   function(jqXHR, textStatus, errorThrown){
                    //alert(textStatus);
                    alert(errorThrown);
                },
    type    :   "POST"
});
  • 写回答

2条回答 默认 最新

  • dousilie9522 2016-08-22 13:13
    关注

    The idea is to indicate that an AJAX call should only be made if there's not another one already being made. Here I indicate it using an extra class on the button which triggers the ajax call.

    Do something like:

    $(selector).on("click",function () {
        if ($(this).is(".pendingResult") { return; }
        $(this).addClass("pendingResult");
        var finally = $.proxy(function () { $(this).removeClass("pendingResult"); }, this);         
    
        $.ajax({
    
            url     :   "pushTelemetry.php",
            cache   :   false,
            data    :   ({
                            DshBrdName  : strFullDashBoardName,
                            DshBrdID    : currentDashboard, 
                            r           : Math.random()
                        }),
            success :   function(data, textStatus, jQxhr){
                            //alert(textStatus); 
                        },
            error   :   function(jqXHR, textStatus, errorThrown){
                            //alert(textStatus);
                            alert(errorThrown);
                        },
            type    :   "POST",
            complete :  finally 
        });
    });
    

    Some further detail:

    JQuery AJAX calls can terminate in either success or error, however whenever either of these happens, the "complete" function is called if specified, this acts like the finally code in a try-catch-finally block.

    However the scope of this "complete" function is the window and not the element which triggered the AJAX call, therefore if you need to somehow pass the clicked element to the "complete" function. Fortunately JQuery has a built-in way to do this using the proxy method. This method accepts a callback and a scope/element and ensures that the "this" object within the callback refers to the specified scoped element. This is similar to the built-in JavaScript method bind, the reason that I used proxy here is that bind is newer and not supported in IE 8 which some people may still be trying to support (for some reason).

    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向