Skip to content

Instantly share code, notes, and snippets.

@ghostcode
Last active December 21, 2022 01:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ghostcode/65b0f7455d48e2375650cc114e13e7c4 to your computer and use it in GitHub Desktop.
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