Question: What is anti-shake?

The function is executed only once within n seconds after the high-frequency event is triggered. If the high-frequency event is triggered again within n seconds, the time is recalculated.

Js implementation

<! DOCTYPEhtml>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="Width = device - width, initial - scale = 1.0">
    <title>Document</title>
</head>
<body>
    <input type="text" id='input'>
    <script>
         /** ** function *@param  {Function} Fn callback function */
        function debounce (fn) {
            const timeout = null
            return function () {
                if (timeout) {
                  clearTimeout(timeout)
                }
                setTimeout(() = > {     
                    fn.apply(this.arguments)},500)}}/** * executes the function */
        function sayHi () {
            console.dir('Anti-shake succeeded')}const inp = document.getElementById('input')
        inp.addEventListener('input', debounce(sayHi))

    </script>
</body>
</html>
Copy the code

The effect