Search
 
SCRIPT & CODE EXAMPLE
 

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"});
Comment

debounce typescript

const debounce = <F extends (...args: any[]) => any>(
  func: F,
  waitFor: number
) => {
  let timeout: ReturnType<typeof setTimeout> | null = null

  const debounced = (...args: Parameters<F>) => {
    if (timeout !== null) {
      clearTimeout(timeout)
      timeout = null
    }
    timeout = setTimeout(() => func(...args), waitFor)
  }

  return debounced as (...args: Parameters<F>) => ReturnType<F>
}

// Usage
const debounceCallback = () => {
  console.log('Debounce')
}

debounce<typeof debounceCallback>(debounceCallback, 500)
Comment

debounce typescript

// Not the best, but doesn't have <any> types, 
// the `let timer` has, but the eslint won't complicate
export default function (fn: () => void, delay = 300) {
  let timer
  return (() => {
    clearTimeout(timer)
    timer = setTimeout(() => fn(), delay)
  })()
}
Comment

PREVIOUS NEXT
Code Example
Typescript :: angular strip html tags pipe 
Typescript :: how to get url parameters snapshots in angular 
Typescript :: write a C proogram to find the roots of quadratic equation 
Typescript :: how to count the number of the digits in an input in python 
Typescript :: convert string to bits c# 
Typescript :: android get digits from string 
Typescript :: check if drive exists c# 
Typescript :: how to delete unused imports intellij webstorm 
Typescript :: react scripts version for react 17.0.2 
Typescript :: mat-sort not working in dynamically generated table 
Typescript :: Emotion: Using both a class and the "css" method in "className" prop 
Typescript :: typescript function as parameter 
Typescript :: failed to enumerate objects in the container access is denied windows 10 
Typescript :: subplots in subplots 
Typescript :: how to make a button that alerts when clicked with html 
Typescript :: create custom objects for user in firebase 
Typescript :: typescript compile on save 
Typescript :: give all element in a list starts with string 
Typescript :: replace element in array typescript 
Typescript :: typescript component props 
Typescript :: draw image html canvas 
Typescript :: calling contract in ether.js 
Typescript :: computed vue typescript 
Typescript :: django model get all documents with a given foreign key 
Typescript :: deleting conflicting outputs 
Typescript :: ignore hosts option in network proxy in ubuntu 16.04 
Typescript :: latest unity version that supports 32 bit 
Typescript :: How to pass optional parameters while omitting some other optional parameters? 
Typescript :: how to get the table contents from a file in python 
Typescript :: matlab components area 
ADD CONTENT
Topic
Content
Source link
Name
8+2 =