Question

I'm trying to use the Instagram API and I'm making AJAX requests in a do-while loop until the next_url is null. All I want this code to do is to fetch all the followers by making continuous requests until it's done. What is wrong in this code?

When I remove the do-while loop it doesn't gives me an error, but as soon as a I use the AJAX request within a loop, it never stops. Clearly the $next_url string is not changing to the newly fetched next_url - why? What is wrong?

$(document).ready(function(e) {
        $('#fetch_followers').click(function(e) {
            var $next_url = 'https://api.instagram.com/v1/users/{user-id}/followed-by?access_token={access-token}&count=100';
            var $access_token = '{access-token}';
            var $is_busy = false;
            var $count = 0;
            do {
                while($is_busy) {}
                $.ajax({
                    method: "GET",
                    url: $next_url,
                    dataType: "jsonp",
                    jsonp : "callback",
                    jsonpCallback: "jsonpcallback",
                    success: function(data) {
                        $is_busy = true;
                        $.each(data.data, function(i, item) {
                            $("#log").val($("#log").val() + item.id + '\n');
                        });
                        $("#log").val($("#log").val() + data.pagination.next_url + '\n');
                        $next_url = data.pagination.next_url;
                    },
                    error: function(jqXHR, textStatus, errorThrown) {
                        $is_busy = true;
                        //alert("Check you internet Connection");
                        $("#log").val($("#log").val() + 'Error\n');
                    },
                    complete: function() {
                        ++$count;
                        $is_busy = false;
                    }
                });
            } while($next_url !== '' || $count <= 50);
        });
    });

After I failed in my logic, I added the $count variable that can break the do-while loop, because the do-while loop was running infinitely. After adding it, it still runs infinitely, and I have no idea why.

Was it helpful?

Solution

Have the function call itself in the ajax success callback with the new url as a parameter:

$(document).ready(function() {
    $('#fetch_followers').click(function() {
        var $access_token = '{access-token}';
        pollInstagram('https://api.instagram.com/v1/users/{user-id}/followed-by?access_token={access-token}&count=100');
    });
});

function pollInstagram(next_url, count) {
    $.ajax({
        method: "GET",
        url: next_url,
        dataType: "jsonp",
        jsonp: "callback",
        jsonpCallback: "jsonpcallback",
        success: function(data) {
            $.each(data.data, function(i, item) {
                $("#log").val($("#log").val() + item.id + '\n');
            });
            $("#log").val($("#log").val() + data.pagination.next_url + '\n');

            // If the next url is not null or blank:
            if( data.pagination.next_url && count <=50 ) {
                pollInstagram(data.pagination.next_url, ++count);
            }
        },
        error: function(jqXHR, textStatus, errorThrown) {
            //alert("Check you internet Connection");
            $("#log").val($("#log").val() + 'Error\n');
        }
    });
}​
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top