Code coverage report for function/throttle.js

Statements: 100% (22 / 22)      Branches: 100% (4 / 4)      Functions: 100% (5 / 5)      Lines: 100% (21 / 21)     

All files » function/ » throttle.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 30 31 32 331       1 3   3 1 1 1   3 188826 188826 188826 188826 188826 7 7 7 188819 3   188826   3     1      
define(['../time/now'], function (now) {
 
    /**
     */
    function throttle(fn, delay){
        var context, timeout, result, args,
            cur, diff, prev = 0;
        function delayed(){
            prev = now();
            timeout = null;
            result = fn.apply(context, args);
        }
        function throttled(){
            context = this;
            args = arguments;
            cur = now();
            diff = delay - (cur - prev);
            if (diff <= 0) {
                clearTimeout(timeout);
                prev = cur;
                result = fn.apply(context, args);
            } else if (! timeout) {
                timeout = setTimeout(delayed, diff);
            }
            return result;
        }
        return throttled;
    }
 
    return throttle;
 
});