Custom debounce function

by Saksham Malhotra

JavaScript

Function.prototype.debounce = function(debounceTime) {
  let timeoutIdentifier;
  return (...args) => {
    clearTimeout(timeoutIdentifier);
    setTimeout(() => {
      this(args)
    }, debounceTime)
  }
}

var debouncedFunction = (function() {
  console.log('hi');
}).debounce(2000);

debouncedFunction();

var obj = {
  name: 'saksham',
  printName: (() => {
    console.log(this.name)
  }).debounce(3000)
}

obj.printName