r3330 committed - autocomplete: implemented delay option

r3330 committed - autocomplete: implemented delay option


Revision: 3330
Author: joern.zaefferer
Date: Tue Sep 29 15:29:20 2009
Log: autocomplete: implemented delay option
http://code.google.com/p/jquery-ui/source/detail?r=3330
Modified:
/branches/dev/tests/visual/autocomplete/default.html
/branches/dev/tests/visual/autocomplete/search.php
/branches/dev/tests/visual/autocomplete/select.html
/branches/dev/ui/jquery.ui.autocomplete.js
=======================================
--- /branches/dev/tests/visual/autocomplete/default.html    Tue Sep 29
14:04:19 2009
+++ /branches/dev/tests/visual/autocomplete/default.html    Tue Sep 29
15:29:20 2009
@@ -26,6 +26,7 @@
        $("#tags").autocomplete({
            source:
["c++", "java", "php", "coldfusion", "javascript", "asp", "ruby", "python", "c", "scala", "groovy", "haskell", "pearl"],
+            delay: 0,
            search: function() {
                log("Searching for: " + this.value);
            },
=======================================
--- /branches/dev/tests/visual/autocomplete/search.php    Tue Sep 29 14:43:01
2009
+++ /branches/dev/tests/visual/autocomplete/search.php    Tue Sep 29 15:29:20
2009
@@ -632,6 +632,8 @@
    if (strpos(strtolower($key), $q) !== false) {
        array_push($result, array("id"=>$value, "label"=>$key));
    }
+    if (count($result) > 12)
+        break;
}
echo array_to_json($result);
=======================================
--- /branches/dev/tests/visual/autocomplete/select.html    Tue Sep 29 14:43:01
2009
+++ /branches/dev/tests/visual/autocomplete/select.html    Tue Sep 29 15:29:20
2009
@@ -28,6 +28,7 @@
                            };
                        });
                    },
+                    delay: 0,
                    change: function(e, ui) {
                        if (!ui.item) {
                            // remove invalid value, as it didn't match anything
=======================================
--- /branches/dev/ui/jquery.ui.autocomplete.js    Tue Sep 29 15:03:21 2009
+++ /branches/dev/ui/jquery.ui.autocomplete.js    Tue Sep 29 15:29:20 2009
@@ -32,14 +32,16 @@
                break;
            default:
                // keypress is triggered before the input value is changed
-                setTimeout(function() {
+                clearTimeout(self.searching);
+                self.searching = setTimeout(function() {
                    self.search(event);
-                }, 13);
+                }, self.options.delay);
                break;
            }
        }).focus(function() {
            self.previous = self.element.val();
        }).blur(function() {
+            clearTimeout(self.searching);
            // clicks on the menu (or a button to trigger a search) will cause a
blur event
            // TODO try to implement this without a timeout, see clearTimeout in
search()
            self.closing = setTimeout(function() {
@@ -168,7 +170,8 @@
});
$.ui.autocomplete.defaults = {
-    minLength: 1
+    minLength: 1,
+    delay: 300
}
})(jQuery);