0a5e1613d3c847396bb1aceda9d19b33db20c435
[platform/framework/web/crosswalk-tizen.git] /
1 var now = require('../time/now');
2
3     /**
4      */
5     function throttle(fn, delay){
6         var context, timeout, result, args,
7             diff, prevCall = 0;
8         function delayed(){
9             prevCall = now();
10             timeout = null;
11             result = fn.apply(context, args);
12         }
13         function throttled(){
14             context = this;
15             args = arguments;
16             diff = delay - (now() - prevCall);
17             if (diff <= 0) {
18                 clearTimeout(timeout);
19                 delayed();
20             } else if (! timeout) {
21                 timeout = setTimeout(delayed, diff);
22             }
23             return result;
24         }
25         throttled.cancel = function(){
26             clearTimeout(timeout);
27         };
28         return throttled;
29     }
30
31     module.exports = throttle;
32
33