param func param wait param immediate export const debounce = ( func , wait , immediate ? ) : ( ( ) => any ) => { let timeout ; let args ; let context ; let timestamp ; let result ; return function ( ) { context = this ; args = arguments ; timestamp = new Date ( ) ; function later ( ) { const now = new Date ( ) ; const last = now . getTime ( ) - timestamp . getTime ( ) ; if ( last < wait ) { timeout = setTimeout ( later , wait - last ) ; } else { timeout = null ; if ( ! immediate ) { result = func . apply ( context , args ) ; } } } const callNow = immediate && ! timeout ; if ( ! timeout ) { timeout = setTimeout ( later , wait ) ; } if ( callNow ) { result = func . apply ( context , args ) ; } return result ; } ; } ;