javascript - Keyout and keyup -
i've found need js function suits needs , wants. however, there 1 minor downside it.
the function waits x milliseconds (ikeydelay) before executes function, bind key event (keyup or keydown). works.
however, when user exits input input loses focus, function executed again. execution should prevented occur, because unnecessary.
my js skills not good, maybe 1 of can me?
$.fn.delayedkey = function(fn, ikeydelay, skeyevent) { var itimeoutid, oeventdata; if (!$.isfunction(fn)) { oeventdata = arguments[0]; fn = arguments[1]; ikeydelay = arguments[2]; skeyevent = arguments[3]; } if (!ikeydelay || 0 > ikeydelay) { ikeydelay = 500; } if (!skeyevent || !this[skeyevent]) { skeyevent = 'keydown'; } function fnexeccallback() { cleartimeout(itimeoutid); fn.apply(this, arguments); } function fndelaycallback() { var = this, args = arguments; cleartimeout(itimeoutid); itimeoutid = settimeout(function() { fnexeccallback.apply(that, args); }, ikeydelay); } if (oeventdata) { this.change(oeventdata, fnexeccallback); this[skeyevent](oeventdata, fndelaycallback); } else { this.change(fnexeccallback); this[skeyevent](fndelaycallback); } return this; };
all fixed! changed following function, , seemed correct issue.
function fnexeccallback(e) { if ( e.type != skeyevent ) return; cleartimeout(itimeoutid); fn.apply(this, arguments); }
here updated jsfiddle.
Comments
Post a Comment