フロントエンドのパフォーマンス最適化-揺れ抑制

アンチシェイクとスロットルは、フロントエンド開発中のパフォーマンス最適化に使用される一般的な方法です。

防振機能(デバウンス):

 function _debounce(fn、delay){

    var delay = delay || 200;
    数時間でした。
    return function(){
        var th = this;
        var args = arguments;
        if(timer){
            clearTimeout(timer);
        }
        timer = setTimeout(function(){
            timer = null;
            fn.apply(th、args);
        }、delay);
    }。
}
 

スロットル機能(スロットル):

function _throttle(fn、interval){
    var last;
    var timer;
    変数の間隔=間隔|| 200;
    return function(){
        var th = this;
        var args = arguments;
        var now = + new Date();
        if(最後&&現在-最後<間隔){
            clearTimeout(timer);
            timer = setTimeout(function(){
                last = now;
                fn.apply(th、args);
            }、interval);
        } else {
            last = now;
            fn.apply(th、args);
        }
    }
}

 

HL
8つのオリジナル記事を公開 Likes5 訪問40,000+

おすすめ

転載: blog.csdn.net/helenwei2017/article/details/89211761