8c5fd47fcc9e8f8fad2f6a856eb120512e31cec3
[platform/framework/web/crosswalk-tizen.git] /
1 define(function () {
2
3     /**
4      * Debounce callback execution
5      */
6     function debounce(fn, threshold, isAsap){
7         var timeout, result;
8         function debounced(){
9             var args = arguments, context = this;
10             function delayed(){
11                 if (! isAsap) {
12                     result = fn.apply(context, args);
13                 }
14                 timeout = null;
15             }
16             if (timeout) {
17                 clearTimeout(timeout);
18             } else if (isAsap) {
19                 result = fn.apply(context, args);
20             }
21             timeout = setTimeout(delayed, threshold);
22             return result;
23         }
24         debounced.cancel = function(){
25             clearTimeout(timeout);
26         };
27         return debounced;
28     }
29
30     return debounce;
31
32 });