Skip to content

Commit

Permalink
Reimplementing timer optimization #1269
Browse files Browse the repository at this point in the history
  • Loading branch information
kritollm committed Dec 5, 2020
1 parent d10beb9 commit 03f9781
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 52 deletions.
36 changes: 18 additions & 18 deletions modules/debounce.js
@@ -1,32 +1,32 @@
import restArguments from './restArguments.js';
import delay from './delay.js';
import now from './now.js';

// When a sequence of calls of the returned function ends, the argument
// function is triggered. The end of a sequence is defined by the `wait`
// parameter. If `immediate` is passed, the argument function will be
// triggered at the beginning of the sequence instead of at the end.
export default function debounce(func, wait, immediate) {
var timeout, result;

var later = function(context, args) {
timeout = null;
if (args) result = func.apply(context, args);
};

var debounced = restArguments(function(args) {
if (timeout) clearTimeout(timeout);
if (immediate) {
var callNow = !timeout;
timeout = setTimeout(later, wait);
if (callNow) result = func.apply(this, args);
var timeout, timestamp, args, result, context;
var later = function () {
var last = now() - timestamp;
if (wait > last) {
timeout = setTimeout(later, wait - last);
} else {
timeout = delay(later, wait, this, args);
timeout = null;
if (!immediate) result = func.apply(context, args);
}
};

var debounced = function () {
var callNow = immediate && !timeout;
context = this;
args = [].slice.call(arguments, 0);
timestamp = now();
if (!timeout) timeout = setTimeout(later, wait);
if (callNow) result = func.apply(context, args);
return result;
});
}

debounced.cancel = function() {
debounced.cancel = function () {
clearTimeout(timeout);
timeout = null;
};
Expand Down
33 changes: 17 additions & 16 deletions underscore-esm.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion underscore-esm.js.map

Large diffs are not rendered by default.

33 changes: 17 additions & 16 deletions underscore.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion underscore.js.map

Large diffs are not rendered by default.

0 comments on commit 03f9781

Please sign in to comment.