Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

手写节流 #2

Open
lovelmh13 opened this issue Jan 29, 2020 · 0 comments
Open

手写节流 #2

lovelmh13 opened this issue Jan 29, 2020 · 0 comments

Comments

@lovelmh13
Copy link
Owner

lovelmh13 commented Jan 29, 2020

/**
* 节流:连续触发了大量的动作,就规定,在某一段时间内,动作执行一次。比如1s内,只执行一次,其他触发动作的时候忽略。技能冷却
*/
function throttle (fn, delay) {
    let sign = true;
    return function () {    // 闭包,保存变量的值,防止每次执行次函数,值都被重置
        if (sign) {
            sign = false;
            setTimeout (() => {
                fn();
                sign = true;
            }, delay);
        } else {
            return false;
        }
    }
}
window.onscroll = throttle(foo, 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant