Throttle

by farhan2056

HTML

<button onclick="throttlePrint(Date.now())">
click
</button>

JavaScript

function throttle(fn,delay){
	let exec = true
	return function(){
  	let ctx = this, args = arguments
    if(exec){
    	fn.apply(ctx,arguments)
      exec = false
      setTimeout(() => {
        exec = true
      },delay)
    }
  }
}

function print(msg){
console.log(msg)
}

let throttlePrint = throttle(print,5000);