• 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})
  • 相关阅读:
    hystrix总结之缓存
    python3列表
    hystrix总结之多返回值命令
    hystrix总结之限流
    hystrix(5) 延时检测
    redis-start
    设计模式-4建造者模式
    设计模式-3原型模式
    设计模式-2工厂设计模式
    设计模式-七大设计原则
  • 原文地址:https://www.cnblogs.com/go4it/p/15400041.html
Copyright © 2020-2023  润新知