Razique Mahroua
2019-11-28 1759c24ad2d2b35ec5c756e3dd3a60185fe944b7
commit | author | age
1759c2 1 /**
RM 2  * angular-strap
3  * @version v2.0.3 - 2014-05-30
4  * @link http://mgcrea.github.io/angular-strap
5  * @author Olivier Louvignes (olivier@mg-crea.com)
6  * @license MIT License, http://www.opensource.org/licenses/MIT
7  */
8 'use strict';
9 angular.module('mgcrea.ngStrap.helpers.debounce', []).constant('debounce', function (func, wait, immediate) {
10   var timeout, args, context, timestamp, result;
11   return function () {
12     context = this;
13     args = arguments;
14     timestamp = new Date();
15     var later = function () {
16       var last = new Date() - timestamp;
17       if (last < wait) {
18         timeout = setTimeout(later, wait - last);
19       } else {
20         timeout = null;
21         if (!immediate)
22           result = func.apply(context, args);
23       }
24     };
25     var callNow = immediate && !timeout;
26     if (!timeout) {
27       timeout = setTimeout(later, wait);
28     }
29     if (callNow)
30       result = func.apply(context, args);
31     return result;
32   };
33 }).constant('throttle', function (func, wait, options) {
34   var context, args, result;
35   var timeout = null;
36   var previous = 0;
37   options || (options = {});
38   var later = function () {
39     previous = options.leading === false ? 0 : new Date();
40     timeout = null;
41     result = func.apply(context, args);
42   };
43   return function () {
44     var now = new Date();
45     if (!previous && options.leading === false)
46       previous = now;
47     var remaining = wait - (now - previous);
48     context = this;
49     args = arguments;
50     if (remaining <= 0) {
51       clearTimeout(timeout);
52       timeout = null;
53       previous = now;
54       result = func.apply(context, args);
55     } else if (!timeout && options.trailing !== false) {
56       timeout = setTimeout(later, remaining);
57     }
58     return result;
59   };
60 });