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 :: typescript quickly pdf 
Typescript :: Simple Bulk insert TSQL csv 
Typescript :: ts declare function type 
Typescript :: typescript remove element from array 
Typescript :: Error response from daemon: Ports are not available: listen tcp 0.0.0.0:3000: bind: Only one usage of each socket address (protocol/network address/port) is normally permitted. 
Typescript :: extend type typescript 
Typescript :: html collection of elements to array 
Typescript :: React.ComponentProps<T 
Typescript :: angular 13 viewchild 
Typescript :: what will the type of empty object in typescript 
Typescript :: emotion/css 
Typescript :: typescript array of object with types 
Typescript :: how to get docker stats using shell script 
Typescript :: empty object typescript 
Typescript :: replace multiple elements in a list python 
Typescript :: jquery select multiple elements with same class 
Typescript :: typescript list concat 
Typescript :: laravel many to many get related posts by category 
Typescript :: create mock promise angular 
Typescript :: typescript loop over enum 
Typescript :: enum as type typescript 
Typescript :: ether.js 
Typescript :: listobjects vba 
Typescript :: array containing objects with matching elements 
Typescript :: execute script when c# code gets executed 
Typescript :: how to Write a program that accepts three decimal numbers as input and outputs their sum on python 
Typescript :: difference between scripted testing and exploratory testing 
Typescript :: angular find and remove from string 
Typescript :: cmd move all files to parent directory 
Typescript :: Start Angular App In Localhost 
ADD CONTENT
Topic
Content
Source link
Name
6+3 =