简体   繁体   English

如何限制Ajax POST请求的数量?

[英]How to limit the number of ajax POST requests?

I am using angular (in case it matters) and am trying to limit the number of ajax POST requests that my browser makes using javascript - ideally vanilla javascript on xmlHttpRequest object. 我正在使用angular(以防万一),并试图限制我的浏览器使用javascript的ajax POST请求的数量-理想情况是xmlHttpRequest对象上的香草javascript。 Any ideas would be greatly appreciated. 任何想法将不胜感激。

Edit 1: 编辑1:

To be more precise, suppose I click a button which typically triggers 15 concurrent POST requests - one for each say, "record". 更准确地说,假设我单击一个按钮,该按钮通常会触发15个并发POST请求-每个说“记录”的请求。 I would like to limit these concurrent requests to be 5 at max. 我想将这些并发请求限制为最多5个。 Is there a low level (ie javascript xmlHttpRequest ) to limit the number of these requests? 是否有一个低级别(即javascript xmlHttpRequest )来限制这些请求的数量? It is also safe to assume the using is always going to be using either chrome or safari as this is an internal tool. 可以肯定,总是使用铬或野生动物园,因为这是内部工具。

You could use interceptors to accomplish this, catching all requests, checking if they're post methods, and stopping the request if some count has been exceeded using configs timeout property. 您可以使用拦截器来完成此任务,捕获所有请求,检查它们是否为发布方法,并使用configs timeout属性来停止请求(如果超出了计数)。

$provide.factory('myHttpInterceptor', function($q, dependency1, dependency2) {
    var countTimes = 0;
    var stopRequest = $q.defer();
    stopRequest.resolve();
    return {
        'request': function(config) {
            if(config.method == 'POST' && countTimes < someMaxNumber) {
                countTimes += 1;
                return config;
            } else {
                config.timeout = stopRequest.promise;
                return config;
        }
    }
});
$httpProvider.interceptors.push('myHttpInterceptor');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM