函数节流

2021-07-06  本文已影响0人  我没叫阿

节流函数:当持续触发事件的时候,保证一段时间内只调用一次事件处理函数。

 <button id="btn">点击</button>
    <script>
        function throttle(fn, wait) {
            let timeOut
            return function () {
                // 如果timeout有值得话,就不执行
                if (!timeOut) {
                    timeOut = setTimeout(() => {
                        fn()
                        timeOut = null
                    }, wait);
                }
            }

        }

        function handle() {
            console.log('是否出现?');
        }
        document.getElementById('btn').onclick = throttle(handle, 2000)
    </script>
上一篇 下一篇

猜你喜欢

热点阅读