极简实现js去抖动,用于优化频繁操作.

来源:互联网 发布:防止流量偷跑软件 编辑:程序博客网 时间:2024/06/05 16:02
<html>    <head>        <title></title>    </head>    <body>    </body></html><script>    window.addEventListener('resize',function(){        console.log('change')    })    function debounce(func,wait){        var timeId = null;        return function(){            //此处巧妙,实现了用户频繁输入,减少执行核心代码次数的功能            clearTimeout(timeId)            timeId = setTimeout(function(){                func()            },wait||200)        }    }    window.addEventListener('resize',debounce(function(a,b,c){        console.log('change')    },3000))</script>
原创粉丝点击