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