Code coverage report for function/debounce.js

Statements: 100% (18 / 18)      Branches: 100% (6 / 6)      Functions: 100% (5 / 5)      Lines: 100% (17 / 17)     

All files » function/ » debounce.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 301         1 4 4 12 12 6 2   6   12 6 6 4   12 12   4     1      
define(function () {
 
    /**
     * Debounce callback execution
     */
    function debounce(fn, threshold, isAsap){
        var timeout, result;
        function debounced(){
            var args = arguments, context = this;
            function delayed(){
                if (! isAsap) {
                    result = fn.apply(context, args);
                }
                timeout = null;
            }
            if (timeout) {
                clearTimeout(timeout);
            } else if (isAsap) {
                result = fn.apply(context, args);
            }
            timeout = setTimeout(delayed, threshold);
            return result;
        }
        return debounced;
    }
 
    return debounce;
 
});