Skip to content

Commit 913bcf2

Browse files
committedDec 7, 2020
Resolved changes requested.
1 parent 769a494 commit 913bcf2

6 files changed

+71
-52
lines changed
 

‎modules/debounce.js

+20-14
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,41 @@
1+
import restArguments from './restArguments.js';
12
import now from './now.js';
23

34
// When a sequence of calls of the returned function ends, the argument
45
// function is triggered. The end of a sequence is defined by the `wait`
56
// parameter. If `immediate` is passed, the argument function will be
67
// triggered at the beginning of the sequence instead of at the end.
78
export default function debounce(func, wait, immediate) {
8-
var timeout, timestamp, args, result, context;
9+
var timeout, previous, args, result, context;
10+
911
var later = function() {
10-
var last = now() - timestamp;
11-
if (wait > last) {
12-
timeout = setTimeout(later, wait - last);
12+
var passed = now() - previous;
13+
if (wait >= passed) {
14+
timeout = setTimeout(later, wait - passed);
15+
1316
} else {
1417
timeout = null;
1518
if (!immediate) result = func.apply(context, args);
19+
// This check is needed because the argument function can recursively invoke debounced
20+
if (!timeout) args = context = null;
1621
}
1722
};
18-
// Remove timer for immediate, samme as for throttle
19-
var debounced = function() {
20-
var callNow = immediate && !timeout;
23+
24+
var debounced = restArguments(function(_args) {
2125
context = this;
22-
args = [].slice.call(arguments, 0);
23-
timestamp = now();
24-
if (!timeout) timeout = setTimeout(later, wait);
25-
if (callNow) result = func.apply(context, args);
26+
args = _args;
27+
previous = now();
28+
if (!timeout) {
29+
timeout = setTimeout(later, wait);
30+
if (immediate) result = func.apply(context, args);
31+
}
2632
return result;
27-
}
33+
});
2834

2935
debounced.cancel = function() {
3036
clearTimeout(timeout);
31-
timeout = null;
37+
timeout = args = context = null;
3238
};
3339

3440
return debounced;
35-
}
41+
}

‎modules/throttle.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export default function throttle(func, wait, options) {
1414
previous = options.leading === false ? 0 : now();
1515
timeout = null;
1616
result = func.apply(context, args);
17+
if (!timeout) context = args = null;
1718
};
1819

1920
var throttled = function() {
@@ -22,14 +23,14 @@ export default function throttle(func, wait, options) {
2223
var remaining = wait - (_now - previous);
2324
context = this;
2425
args = arguments;
25-
if (remaining <= 0) {
26+
if (remaining <= 0 || remaining > wait) {
2627
if (timeout) {
2728
clearTimeout(timeout);
2829
timeout = null;
2930
}
3031
previous = _now;
3132
result = func.apply(context, args);
32-
//if (!timeout) context = args = null;
33+
if (!timeout) context = args = null;
3334
} else if (!timeout && options.trailing !== false) {
3435
timeout = setTimeout(later, remaining);
3536
}
@@ -39,8 +40,8 @@ export default function throttle(func, wait, options) {
3940
throttled.cancel = function() {
4041
clearTimeout(timeout);
4142
previous = 0;
42-
timeout = null;
43+
timeout = context = args = null;
4344
};
4445

4546
return throttled;
46-
}
47+
}

‎underscore-esm.js

+22-16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎underscore-esm.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎underscore.js

+22-16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎underscore.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
Please sign in to comment.