function debounce(key, action, delay) {
    let now = new Date().getTime();
    let d = debouncer[key];
    if(!d) {
        d = {
            lastrun: 0,
            timeout: null,
        };
        debouncer[key] = d;
    }

    if(d.lastrun+delay < now) {
        //hasn't run (for a while).. run it immediately
        d.lastrun = now;
        action();
    } else {
        //debounce
        if(!d.timeout) {
            let need_delay = d.lastrun + delay - now;
            d.timeout = setTimeout(action, need_delay);
            d.lastrun = now + need_delay;
            setTimeout(()=>{
                d.timeout = null;
            }, need_delay);
        }
    }
}
//run a given action as frequently as the specified delay
run a given action as frequently as the specified delay
run a given action as frequently as the specified delay
function debounce(key, action, delay) {
    let now = new Date().getTime();
    let d = debouncer[key];
    if(!d) {
        d = {
            lastrun: 0,
            timeout: null,
        };
        debouncer[key] = d;
    }
    if(d.lastrun+delay < now) {
        d.lastrun = now;
        action();
    } else {
        if(!d.timeout) {
            let need_delay = d.lastrun + delay - now;
            d.timeout = setTimeout(action, need_delay);
            d.lastrun = now + need_delay;
            setTimeout(()=>{
                d.timeout = null;
            }, need_delay);
        }
    }
}
function debounce ( key , action , delay ) { let now = new Date ( ) . getTime ( ) ; let d = debouncer [ key ] ; if ( ! d ) { d = { lastrun : 0 , timeout : null , } ; debouncer [ key ] = d ; } if ( d . lastrun + delay < now ) { d . lastrun = now ; action ( ) ; } else { if ( ! d . timeout ) { let need_delay = d . lastrun + delay - now ; d . timeout = setTimeout ( action , need_delay ) ; d . lastrun = now + need_delay ; setTimeout ( ( ) => { d . timeout = null ; } , need_delay ) ; } } }
run a given action as frequently as the specified delay
run a given action as frequently as the specified delay
//hasn't run (for a while).. run it immediately //debounce