title: Keyout and keyup
body: <p>I've found a need JS function that perfectly suits all my needs and wants. However, there is one minor downside to it. </p>
  
  <p>The function waits for x milliseconds (iKeyDelay) before executes a function, all this is bind to a key event (keyup or keydown). This all works. </p>
  
  <p>However, when the user exits the input so the input loses the focus, the function is executed again. This extra execution should be prevented to occur, because it is unnecessary. </p>
  
  <p>My JS skills are not that good, but maybe one of you can help me? </p>
  
  <pre><code>$.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 &gt; iKeyDelay) {
        iKeyDelay = 500;
     }
  
     if (!sKeyEvent || !this[sKeyEvent]) {
        sKeyEvent = 'keydown';
     }
  
     function fnExecCallback() {
        clearTimeout(iTimeoutId);
        fn.apply(this, arguments);
     }
  
     function fnDelayCallback() {
        var that = 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;
  };
  </code></pre>