javascript 如何限制ajax请求?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5031501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 15:43:58  来源:igfitidea点击:

How to rate-limit ajax requests?

javascriptjquery

提问by allis

There are several divs and handler to send ajax requests when they are clicked. My problem is that i don't know how to force my handler not to exceed limit of 1 request per 30 seconds.

有几个 div 和处理程序可以在单击时发送 ajax 请求。我的问题是我不知道如何强制我的处理程序不超过每 30 秒 1 个请求的限制。

Appreciate your help!

感谢你的帮助!

回答by Wayne

The excellent Underscore.jshas a throttle function. You pass in the handler that you want to throttle and get back a rate-limited version of the same function.

优秀的Underscore.js有节流功能。您传入要限制的处理程序并取回同一函数的限速版本。

var throttled = _.throttle(someHandler, 100);
$(div).click(throttled);

http://documentcloud.github.com/underscore/#throttle

http://documentcloud.github.com/underscore/#throttle

Here's a simplified version that I've used in my own code:

这是我在自己的代码中使用的简化版本:

function throttle(func, wait) {
    var timeout;
    return function() {
        var context = this, args = arguments;
        if (!timeout) {
            // the first time the event fires, we setup a timer, which 
            // is used as a guard to block subsequent calls; once the 
            // timer's handler fires, we reset it and create a new one
            timeout = setTimeout(function() {
                timeout = null;
                func.apply(context, args);
            }, wait);
        }
    }
}

A good way to test it is by firing off a bunch of scroll events and watching your handler log to the Firebug console:

测试它的一个好方法是触发一堆滚动事件并观察您的处理程序日志到 Firebug 控制台:

document.addEventListener("scroll", throttle(function() {
    console.log("test");
}, 2000), false); 

Here's a version that limits click-events on divs to once every 30 seconds, as requested (requires jQuery):

这是一个div根据要求将 s上的点击事件限制为每 30 秒一次的版本(需要 jQuery):

$("div").click(throttle(function() {
    // ajax here
}, 30000));

回答by Matthew O'Riordan

If you want to rate limit, then unfortunately the _.throttle method that underscore.js provides is not your solution. Throttle will simply ensure your method is never called more than X seconds, and therefore all subsequent function calls will be disregarded until that period has passed.

如果您想限制速率,那么不幸的是 underscore.js 提供的 _.throttle 方法不是您的解决方案。Throttle 只会确保您的方法不会被调用超过 X 秒,因此所有后续函数调用都将被忽略,直到该时间段过去。

If you want to rate limit so that you never call your function more than X times per second, but don't lose those function calls altogether, then you need a wholly different solution.

如果您想限制速率,以便每秒调用函数的次数不会超过 X 次,但又不想完全丢失这些函数调用,那么您需要一个完全不同的解决方案。

I have written an underscore extension at https://gist.github.com/1084831

我在https://gist.github.com/1084831写了一个下划线扩展

You can see a working example at http://jsbin.com/upadif/8/edit#preview

您可以在http://jsbin.com/upadif/8/edit#preview 上看到一个工作示例

回答by Mohamad

Create a boolean canFireRequest, or whatever, flag and set it to false after each ajax request. Then create a 30 second time span that sets it back to true; check the flag's value before each new request.

在每个 ajax 请求之后创建一个布尔值 canFireRequest 或其他标记并将其设置为 false。然后创建一个 30 秒的时间跨度,将其设置回 true;在每个新请求之前检查标志的值。

Here's a rough example:

这是一个粗略的例子:

if ($(this).data('canFireRequest')) {
    // Ajax request goes here
    $(this).data('canFireRequest', false);
}

setTimeout(function() {
    $(this).data('canFireRequest', true)
}, 30000);