Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR TYPESCRIPT

debounce typescript

// DECLARE DEBOUNCE FUNCTION
debounce = <T extends (...args: any[]) => any>(
	callback: T,
	waitFor: number
) => {
	let timeout: ReturnType<typeof setTimeout>;
    return (...args: Parameters<T>): ReturnType<T> => {
      let result: any;
      timeout && clearTimeout(timeout);
      timeout = setTimeout(() => {
      	result = callback(...args);
      }, waitFor);
      return result;
    };
};
// USE DEBOUNCE FUNCTION
debounce((data) => {
	// DO THE JOB
	this.job(data);
}, 50)({"data":"myData"});
 
PREVIOUS NEXT
Tagged: #debounce #typescript
ADD COMMENT
Topic
Name
2+8 =