delay in hinter

master
Vitaliy Filippov 2013-01-09 15:30:44 +00:00
parent ea6f19fac9
commit 48c06cced3
1 changed files with 27 additions and 9 deletions

View File

@ -6,7 +6,7 @@
Usage: Usage:
Include hinter.css, hinter.js on your page. Then write: 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: Parameters:
input input
@ -17,7 +17,7 @@
'hint' parameter will be this autocompleter object, and the guess 'hint' parameter will be this autocompleter object, and the guess
should be done based on 'value' parameter (string). should be done based on 'value' parameter (string).
Optional parameters: params attribute is an object with optional parameters:
multipleDelimiter multipleDelimiter
Pass a delimiter string (for example ',' or ';') to enable multiple selection. Pass a delimiter string (for example ',' or ';') to enable multiple selection.
Item values cannot have leading or trailing whitespace. Input value will consist 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. If emptyText === false, the hint will be hidden instead of showing text.
allowHTML allowHTML
If true, HTML code will be allowed in option names. 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 *** // *** Constructor ***
var SimpleAutocomplete = function(input, dataLoader, multipleDelimiter, onChangeListener, maxHeight, emptyText, allowHTML) var SimpleAutocomplete = function(input, dataLoader, params)
{ {
if (typeof(input) == 'string') if (typeof(input) == 'string')
input = document.getElementById(input); input = document.getElementById(input);
if (emptyText === undefined) if (emptyText === undefined)
emptyText = 'No items found'; emptyText = 'No items found';
if (!params)
params = {};
// Parameters // Parameters
this.input = input; this.input = input;
this.multipleDelimiter = multipleDelimiter;
this.dataLoader = dataLoader; this.dataLoader = dataLoader;
this.onChangeListener = onChangeListener; this.multipleDelimiter = params.multipleDelimiter;
this.maxHeight = maxHeight; this.onChangeListener = params.onChangeListener;
this.emptyText = emptyText; this.maxHeight = params.maxHeight;
this.allowHTML = allowHTML; this.emptyText = params.emptyText;
this.allowHTML = params.allowHTML;
this.delay = params.delay;
if (this.delay === undefined)
this.delay = 300;
// Variables // Variables
this.timer = null;
this.closure = []; this.closure = [];
this.items = []; this.items = [];
this.skipHideCounter = 0; this.skipHideCounter = 0;
@ -378,7 +387,16 @@ SimpleAutocomplete.prototype.onChange = function()
if (v != this.curValue) if (v != this.curValue)
{ {
this.curValue = v; 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; return true;
}; };