diff --git a/hinter.js b/hinter.js index 375eeb1..a712df6 100644 --- a/hinter.js +++ b/hinter.js @@ -6,7 +6,7 @@ Usage: Include hinter.css, hinter.js on your page. Then write: - var hint = new SimpleAutocomplete(input, dataLoader, multipleDelimiter, onChangeListener, maxHeight, emptyText, allowHTML); + var hint = new SimpleAutocomplete(input, dataLoader, params); Parameters: input @@ -17,7 +17,7 @@ 'hint' parameter will be this autocompleter object, and the guess should be done based on 'value' parameter (string). - Optional parameters: + params attribute is an object with optional parameters: multipleDelimiter Pass a delimiter string (for example ',' or ';') to enable multiple selection. Item values cannot have leading or trailing whitespace. Input value will consist @@ -36,27 +36,36 @@ If emptyText === false, the hint will be hidden instead of showing text. allowHTML If true, HTML code will be allowed in option names. + delay + If this is set to a non-zero value, the autocompleter does no more than + 1 request in each delay milliseconds. */ // *** Constructor *** -var SimpleAutocomplete = function(input, dataLoader, multipleDelimiter, onChangeListener, maxHeight, emptyText, allowHTML) +var SimpleAutocomplete = function(input, dataLoader, params) { if (typeof(input) == 'string') input = document.getElementById(input); if (emptyText === undefined) emptyText = 'No items found'; + if (!params) + params = {}; // Parameters this.input = input; - this.multipleDelimiter = multipleDelimiter; this.dataLoader = dataLoader; - this.onChangeListener = onChangeListener; - this.maxHeight = maxHeight; - this.emptyText = emptyText; - this.allowHTML = allowHTML; + this.multipleDelimiter = params.multipleDelimiter; + this.onChangeListener = params.onChangeListener; + this.maxHeight = params.maxHeight; + this.emptyText = params.emptyText; + this.allowHTML = params.allowHTML; + this.delay = params.delay; + if (this.delay === undefined) + this.delay = 300; // Variables + this.timer = null; this.closure = []; this.items = []; this.skipHideCounter = 0; @@ -378,7 +387,16 @@ SimpleAutocomplete.prototype.onChange = function() if (v != this.curValue) { this.curValue = v; - this.dataLoader(this, v); + if (!this.delay) + this.dataLoader(this, v); + else if (!this.timer) + { + var self = this; + this.timer = setTimeout(function() { + self.dataLoader(self, self.curValue); + self.timer = null; + }, this.delay); + } } return true; };