-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobserve_field.js
30 lines (27 loc) · 921 Bytes
/
observe_field.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
/** observe input field for content changes every frequency seconds since last keydown.
Initial version is https://github.com/splendeo/jquery.observe_field, slightly improved to works
nice in my projects.
*/
jQuery.fn.observe_field = function(frequency, callback) {
return this.each(function(){
var element = $(this);
var prev = element.val();
var chk = function() {
var val = element.val();
if(prev != val){
prev = val;
element.map(callback); // invokes the callback on the element
}
};
frequency = frequency * 1000; // translate to milliseconds
element.bind('blur',function() {
this.ti && clearInterval(this.ti);
});
this.ti = setInterval(chk, frequency);
// reset counter after user interaction
element.bind('keyup', function() {
this.ti && clearInterval(this.ti);
this.ti = setInterval(chk, frequency);
});
});
};