Django-ajax多个请求。

I am working with django-ajax module and I have some ajax based work:

views.py

@ajax
@login_required
def search_dist(request):
    dist_list = Distributor.objects.all()
    if request.method == 'GET':
        starts_with = request.GET['query']

    if starts_with:
        dist_list = Distributor.objects.filter(
            surname__istartswith=starts_with)
    return render(request, 'distributors/distributors_list.html', {'distributors': dist_list})

JS

var search = $('#search');
search.on('keyup', function() {
    window.setTimeout(function() {
        var query;
        query = search.val();
        ajaxGet('{% url "distributors:search_dist" %}', {
            'query': query
        }, function(content) {
            $('#distributors').html(content);
            set_favorite();
        })
    }, 1000);
});

It works just fine, but when I open my console I see that server makes too many requests for the same thing. For example: after I input 'Jhon' I see [03/Jun/2015 10:56:36]"GET /distributors/search_dist?query= HTTP/1.1" 200 25 times. Is something wrong with my code or it's just normal?

I see you've only used setTimeout. But you'll need to use clearTimeout function, too.

search.on('keyup', function() {
    var timer;
    clearTimeout(timer);
    var query = search.val();
    timer = setTimeout(function() {
        ajaxGet('{% url "distributors:search_dist" %}', {
            'query': query
        }, function(content) {
            $('#distributors').html(content);
            set_favorite();
        })
    }, 1000);
});