Konstantinos-infogeek
7/1/2015 - 12:00 AM

Watch function

Watch function

var watch = function (eventType, element, callback, time) {
    var handler = false;
    
    //set a default time value
    time = typeof time !== 'undefined' ? time : 2000;

    var watcher = function (evt) {
        clearTimeout(handler)
        handler = setTimeout(callback.bind(element), time);
    }

    element.addEventListener(eventType, watcher);
};

//Usage

 var text = document.getElementById('test');
 
 watch('keyup', text, function () {
      console.log(this.value);
  });