Q:

Typeahead request for remote data everytime key pressed. Is there a way to arrange preventing making parallel requests ?

I have the following typeahead function 
$('.search-input').typeahead({
minLength: 3,
source: function (query, process) {
    return $.getJSON(
        '/search/search_autocomplete/',
        {query: query},
        function (data) {
            var newData = [];
            $.each(data,function(){
                newData.push(this.destination + "  (" + this.country + ")");
            });
            return process(newData)
        })

}

});
But everytime when I press a key after 3 chars, it goes and make a get request from server. And after a while, all the requests stuck in pending, and those parallel requests becomes a serious load for the server to response. Is there a way to arrange the parallel requests or wait for some time interval before making requests. 

A:

try something like this:
var running = 0;

$('.search-input').typeahead({
minLength: 3,
source: function (query, process) {
    if(running) return;
    running = 1; 

    return $.getJSON(
        '/search/search_autocomplete/',
        {query: query},
        function (data) {
            var newData = [];
            $.each(data,function(){
                newData.push(this.destination + "  (" + this.country + ")");
            });
            return process(newData)
        }).always(running=0);

}

you also could try a timeout in the alway() function: setInterval(function(){running=0},500);

