zoukankan      html  css  js  c++  java
  • JavaScript 常用函数 通用函数

    <!DOCTYPE html><html><head><meta charset="utf-8"><title>函数节流 Function throttling</title></head> 
    <body>函数节流 Function throttling ......<script>
    
    //函数节流
    let throttle= function(fnToBeExecuted,delay){
        let timer;
        let firstTime = true;
        
        return function(){
            let closureThis = this;
            let closureArgs = arguments;
            
            if(timer){
                return false;
            }
            if(firstTime){
                fnToBeExecuted.apply(closureThis,closureArgs);
                firstTime = false;
            }
            
            timer = setTimeout(function(){
                clearTimeout(timer);
                timer = null;
                fnToBeExecuted.apply(closureThis,closureArgs);
            },(delay || 500));
        }
    }
    
    window.onresize = throttle(function(){console.log('throttle')},3000);
    
    </script></body></html>
    // 通用的单例模式
    let getSingleton = function(fn)(){
        let instance;
        return function(){
            return instance || (instance = fn.apply(this,arguments));
        }
    };
    getSingleton(function(){//createSomethingThenReturn})
  • 相关阅读:
    洛谷 P6622
    洛谷 P6619
    LOJ 3188
    CF::Gym 102174G
    eJOI2017~2019
    洛谷 P6313
    洛谷 P6305
    JSOI2020 酱油记
    洛谷 P6234
    CodeForces 1334F
  • 原文地址:https://www.cnblogs.com/go4it/p/15400041.html
Copyright © 2011-2022 走看看