source: frontend/node_modules/underscore/modules/debounce.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 11 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 1.2 KB
Line 
1import restArguments from './restArguments.js';
2import now from './now.js';
3
4// When a sequence of calls of the returned function ends, the argument
5// function is triggered. The end of a sequence is defined by the `wait`
6// parameter. If `immediate` is passed, the argument function will be
7// triggered at the beginning of the sequence instead of at the end.
8export default function debounce(func, wait, immediate) {
9 var timeout, previous, args, result, context;
10
11 var later = function() {
12 var passed = now() - previous;
13 if (wait > passed) {
14 timeout = setTimeout(later, wait - passed);
15 } else {
16 timeout = null;
17 if (!immediate) result = func.apply(context, args);
18 // This check is needed because `func` can recursively invoke `debounced`.
19 if (!timeout) args = context = null;
20 }
21 };
22
23 var debounced = restArguments(function(_args) {
24 context = this;
25 args = _args;
26 previous = now();
27 if (!timeout) {
28 timeout = setTimeout(later, wait);
29 if (immediate) result = func.apply(context, args);
30 }
31 return result;
32 });
33
34 debounced.cancel = function() {
35 clearTimeout(timeout);
36 timeout = args = context = null;
37 };
38
39 return debounced;
40}
Note: See TracBrowser for help on using the repository browser.