current position:Home>Front end throttling and anti shake

Front end throttling and anti shake

2022-06-24 09:43:44You are far from it.

  1. Shake proof
  		//  Shake proof 
        function debuonce(fn, delay) {
    
            let timer = null
            return function () {
    
                if (timer) {
    
                    clearTimeout(timer)
                }
                timer = setTimeout(() => {
    
                    fn()
                }, delay)
            }
        }
  1. throttle
 			function debuonce(fn, delay) {
    
            let flag = true
            return function (fn, delay) {
    
                if (!flag) {
    
                    return false
                }
                flag = false
                setTimeout(() => {
    
                    fn()
                    flag = true
                }, delay)
            }
        }

copyright notice
author[You are far from it.],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/175/202206240824353435.html

Random recommended