Skip to content

Commit 03f9781

Browse files
committedDec 5, 2020
Reimplementing timer optimization #1269
1 parent d10beb9 commit 03f9781

File tree

5 files changed

+54
-52
lines changed

5 files changed

+54
-52
lines changed
 

‎modules/debounce.js

+18-18
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
1-
import restArguments from './restArguments.js';
2-
import delay from './delay.js';
1+
import now from './now.js';
32

43
// When a sequence of calls of the returned function ends, the argument
54
// function is triggered. The end of a sequence is defined by the `wait`
65
// parameter. If `immediate` is passed, the argument function will be
76
// triggered at the beginning of the sequence instead of at the end.
87
export default function debounce(func, wait, immediate) {
9-
var timeout, result;
10-
11-
var later = function(context, args) {
12-
timeout = null;
13-
if (args) result = func.apply(context, args);
14-
};
15-
16-
var debounced = restArguments(function(args) {
17-
if (timeout) clearTimeout(timeout);
18-
if (immediate) {
19-
var callNow = !timeout;
20-
timeout = setTimeout(later, wait);
21-
if (callNow) result = func.apply(this, args);
8+
var timeout, timestamp, args, result, context;
9+
var later = function () {
10+
var last = now() - timestamp;
11+
if (wait > last) {
12+
timeout = setTimeout(later, wait - last);
2213
} else {
23-
timeout = delay(later, wait, this, args);
14+
timeout = null;
15+
if (!immediate) result = func.apply(context, args);
2416
}
17+
};
2518

19+
var debounced = function () {
20+
var callNow = immediate && !timeout;
21+
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);
2626
return result;
27-
});
27+
}
2828

29-
debounced.cancel = function() {
29+
debounced.cancel = function () {
3030
clearTimeout(timeout);
3131
timeout = null;
3232
};

‎underscore-esm.js

+17-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

+17-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.