21

I use twitter bootstrap typeahead for displaying some data from server, the problem is when user types an letter typeahead makes an ajax call to server and when I try to type 2 or 3 letters fast it will make 2 or 3 ajax calls to server, is there a possibility to make an delay when user is typing letters to make only one ajax call to server

my code at the moment:

$('#SEARCH-INPUT').typeahead({
                            source: function (query, process) {

                                jQuery.ajax({
                                    type: "GET",
                                    url: "/organisations/search",
                                    data: {search_name: $("#SEARCH-INPUT").val()},
                                    beforeSend: function(){
                                        $("#SEARCH-LOADING").show();
                                    },
                                    success: function (data) {
                                        organisations = [];
                                        organisations_hash = {};
                                      $.each(data, function(i, item){
                                        organisations_hash[item.name.toUpperCase()] = item.id;
                                        organisations.push(item.name);

                                      });
                                      organisations.sort();

                                      $("#SEARCH-LOADING").addClass("hidden");
                                      process(organisations);
                                    },
                                    error: function (){ alert("error");},
                                    async:   false
                                    });
                            }
                            ,updater: function (item) {
                                $("#SEARCH-INPUT").val(item);
                                $("#SEARCH-BUTTON").click();
                                return item;
                            }
                            ,items: 9
                            ,minLength: 1
                       });
Maki
  • 913
  • 4
  • 13
  • 22

1 Answers1

45

Use this solution:

(function() {
    var timeout;

    $('[name="fill"]').typeahead({
        source: function(value) {
            if (timeout) {
                clearTimeout(timeout);
            }

            timeout = setTimeout(function() {
                alert(value);
            }, 300);
        }
    });
})();

See in action:

http://jsfiddle.net/nnPVn/

setTimeout and clearTime works fine!

MrBoolean
  • 601
  • 7
  • 13
  • 1
    It's got one flaw - if you have a minLength set, then when you quickly backspace through the entered text and clear the input, then the timeout stil ends up firing. I would expect it not to... see this example: http://jsfiddle.net/nnPVn/16/ – Simon Green Mar 02 '14 at 19:23
  • 1
    The question above doesn't have this problem cause the minLength is 1 (the default). – MrBoolean Mar 04 '14 at 16:32