0

I have the following function:

function getData(a,url) {
        var deferreds = [];
        var i = 1;
        for (i = 1; i <= a.length; i++) {
            var count = i;
            var back = a[i]['link'];
            var path = "http://example.com";
            deferreds.push(
            $.ajax({
                url:path,
                data:{back:back,link:url},
                type:"POST",
                async: true,
                delay: count
            }).done(function(data) {
                //success function here
            }));
        }


        return deferreds;
    }

My question is how to make this script to run a queue, for example I have 2000 requests, how to put them in a queue of 100 one by one?

7
  • I don't understand. Javascript will fire all request at the same time. Do you want that it fires them in blocks of 100? Commented May 13, 2015 at 13:13
  • YES! that's the point. To make a queue or make them in blocks Commented May 13, 2015 at 13:14
  • In this for loop? I mean, a.length is the number of simultaneous requests? Commented May 13, 2015 at 13:15
  • I don't know how to make it use blocks or to make a queue for it. Maybe I don't need that in the for loop or I won't need a for loop at all. Commented May 13, 2015 at 13:18
  • If you mean that each consecutive request should be initiated only when the previous request is completed, then this is impossible with Deferreds. The only way is recursion. Commented May 13, 2015 at 13:18

1 Answer 1

1

Maybe in this way (Of course is a simplification)

var completed = 0;
var limit_simultaneous = 100;
var total_requests = 2134 // Assign your var

function send_block() {
    for (i = 0; i < limit_simultaneous; i++) {
        $.ajax({
            url:path,
            data:{back:back,link:url},
            type:"POST",
            async: true,
        }).done(function(data) {
           completed++;
           send_next_block();
           //success function here
        }));
    }
}

function send_next_block() 
{
    if (completed == limit_simultaneous && total_requests > 0) {
        total_requests = total_requests - completed;
        if (total_requests < limit_simultaneous) {
            limit_simultaneous = total_requests;
        }
        completed = 0;
        // Fire again
        send_block(); // Send another 100
    }
}

I hope it helps.

EDIT Edit to take account about the total requests. Maybe is not a working code, but it is the idea.

Sign up to request clarification or add additional context in comments.

5 Comments

How do you check this if the complete number is equal to the a.length number to stop the function? Can it work like to make an if in the done function and check the complete number with the a.length and if its smaller to continue and if not to stop?
I don't understand you, Alexander. In my code, limit_simultaneous = a.length. You can do the assignment maybe before the loop.
for a.length = 2113 how do I tell the function to stop after it goes trough all of the 2113 items on blocks of 100?
The only problem left is the back parameter is like a[i]['param'] where i is the number of request. I think making an algorithm like adding 100 to i and 100 to limit will do the trick
Do an global counter of requests.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.