Advertisement
Guest User

Untitled

a guest
Mar 28th, 2015
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. $.fn.DelayedKey = function(fn, iKeyDelay, sKeyEvent) {
  2. var iTimeoutId,
  3. oEventData;
  4.  
  5. if (!$.isFunction(fn)) {
  6. oEventData = arguments[0];
  7. fn = arguments[1];
  8. iKeyDelay = arguments[2];
  9. sKeyEvent = arguments[3];
  10. }
  11.  
  12. if (!iKeyDelay || 0 > iKeyDelay) {
  13. iKeyDelay = 500;
  14. }
  15.  
  16. if (!sKeyEvent || !this[sKeyEvent]) {
  17. sKeyEvent = 'keydown';
  18. }
  19.  
  20. function fnExecCallback() {
  21. clearTimeout(iTimeoutId);
  22. fn.apply(this, arguments);
  23. }
  24.  
  25. function fnDelayCallback() {
  26. var that = this,
  27. args = arguments;
  28. clearTimeout(iTimeoutId);
  29. iTimeoutId = setTimeout(function() {
  30. fnExecCallback.apply(that, args);
  31. }, iKeyDelay);
  32. }
  33.  
  34. if (oEventData) {
  35. this.change(oEventData, fnExecCallback);
  36. this[sKeyEvent](oEventData, fnDelayCallback);
  37. }
  38. else {
  39. this.change(fnExecCallback);
  40. this[sKeyEvent](fnDelayCallback);
  41. }
  42.  
  43. return this;
  44. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement