unliar / unliar.github.io

一个已经不再使用的静态博客,新的博客在后边。
https://happysooner.com
0 stars 0 forks source link

实现一个节流函数。 #30

Open unliar opened 3 years ago

unliar commented 3 years ago

节流函数永远只执行第一个被激活的函数。

function throttle(func, wait) {
    var timeout;
    return function() {
        context = this;
        args = arguments;

        if (!timeout) {
            timeout = setTimeout(function(){
                timeout = null;
                func.apply(context, args)
            }, wait)
        }

    }
}