forked from tkazec/final--jquery-keystop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.keystop.js
38 lines (31 loc) · 1.05 KB
/
jquery.keystop.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*\
* jQuery keystop plugin (https://github.com/tkazec/keystop)
* Copyright (c) 2012 Teddy Cross (http://tkaz.ec), shared under the MIT (http://www.opensource.org/licenses/mit-license.php)
*
* Adds a "keystop" event which triggers once the user has stopped typing in an input for a specified period of time.
* .keystop(callback, [delay]) to bind / .keystop() to trigger / .off("keystop") to unbind
\*/
(function ($) { "use strict";
$.event.special.keystop = {
add: function (details) {
var $el = $(this);
var ns = ".__" + details.guid;
var delay = details.data || 500;
var tID = -1;
details.namespace += ns;
$el.on("input" + ns + " propertychange" + ns, function () {
clearTimeout(tID);
tID = setTimeout(function () {
$el.trigger("keystop" + ns);
}, delay);
});
},
remove: function (details) {
var ns = ".__" + details.guid;
$(this).off("input" + ns + " propertychange" + ns);
}
};
$.fn.keystop = function (handler, delay) {
return handler ? this.on("keystop", delay, handler) : this.trigger("keystop");
};
})(jQuery);