Skip to content

Instantly share code, notes, and snippets.

@ghostcode
Last active December 21, 2022 01:27
Show Gist options
  • Select an option

  • Save ghostcode/65b0f7455d48e2375650cc114e13e7c4 to your computer and use it in GitHub Desktop.

Select an option

Save ghostcode/65b0f7455d48e2375650cc114e13e7c4 to your computer and use it in GitHub Desktop.
throttle

·· function throttle(fn,wait=3000){ let lastTime = Date.now(), timeFlag = null

	return function(...args){
		let current = Date.now()
		
		clearTimeout(timeFlag)
		
		if(current - lastTime >= wait){
			fn.apply(this,args)
			lastTime = current
		}else{
			timeFlag = setTimeout(()=>{
				fn.apply(this,args)
				lastTime = current
			},wait)
		}
	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment