zoukankan      html  css  js  c++  java
  • js setTimeout 防抖

            !function(){
                const $div = document.querySelector('#oDiv');
                let interval = 2000
                let t0 = new Date().getTime() + interval
                let tmr = null
    
                function updateTime(){
                    let now = new Date().getTime()
                    let span = Math.max(0, interval - (now - t0))
                    t0 += interval
    
                    $div.innerHTML = `${now} ${span}`
                    
                    tmr = setTimeout(() => {
                        updateTime()
                    }, span);
                }
                updateTime()
            }()
    
    /**
     * Self-adjusting interval to account for drifting
     * 
     * @param {function} workFunc  Callback containing the work to be done
     *                             for each interval
     * @param {int}      interval  Interval speed (in milliseconds) - This 
     * @param {function} errorFunc (Optional) Callback to run if the drift
     *                             exceeds interval
     */
    function AdjustingInterval(workFunc, interval, errorFunc) {
        var that = this;
        var expected, timeout;
        this.interval = interval;
    
        this.start = function() {
            expected = Date.now() + this.interval;
            timeout = setTimeout(step, this.interval);
        }
    
        this.stop = function() {
            clearTimeout(timeout);
        }
    
        function step() {
            var drift = Date.now() - expected;
            if (drift > that.interval) {
                // You could have some default stuff here too...
                if (errorFunc) errorFunc();
            }
            workFunc();
            expected += that.interval;
            timeout = setTimeout(step, Math.max(0, that.interval-drift));
        }
    }
    
  • 相关阅读:
    hdu 4107 Gangster 线段树(成段更新)
    hdu 3037 Saving Beans (lucas定理)
    hdu 3944 DP? (Lucas 定理)
    hdu 5038 Grade 水
    ASP.NET Core 配置 MVC
    ASP.NET Core 静态文件
    ASP.NET Core 异常和错误处理
    ASP.NET Core 中间件
    ASP.NET Core 项目配置 ( Startup )
    ASP.NET Core 基本项目目录结构
  • 原文地址:https://www.cnblogs.com/zh33gl/p/11660909.html
Copyright © 2011-2022 走看看