The logic of throttling: When the same task is executed several times, the number of tasks should be reduced to the same time at each interval. Code logic: take each click time, subtract from the last execution time: if the wait time is greater than the execution time. Code:
<button id="thorttle"> throttling < / button ><script>
const thorttle = document.getElementById('thorttle');
function fn(){
console.log(Date.now());
}
function thorttleFunction(fun){
let pre = 0;
return function(){
let now = Date.now();
if(now-pre>1000){
fun();
pre= now;
}
}
}
thorttle.addEventListener('click',thorttleFunction(fn));
</script>
</body>
Copy the code