Skip to content

Commit

Permalink
fix(FEC-10811): floating show two bottom bar for second (#599)
Browse files Browse the repository at this point in the history
Issue: the first event of resize/scroll doesn't take into account.
Solution: trigger the function on the leading edge instead of the trail.
  • Loading branch information
Yuvalke committed May 12, 2021
1 parent bad32ec commit ca8e2f2
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/utils/debounce.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* This function is built in order to limit the amount of times a function is called in a certain time frame
* @param {Component} origFunc - the original function to be called
* @param {Function} time - the time frame to allow only one function call
* @param {boolean} immediate - trigger the function on the leading edge, instead of the trailing
* @returns {Function} the wrapped debounce function
* @example
* this.props.eventManager.listen(
Expand All @@ -13,13 +14,20 @@
* }, ON_WINDOW_RESIZE_DEBOUNCE_DELAY)
* );
*/
export const debounce: Function = (origFunc: Function, time: number) => {
export const debounce: Function = (origFunc: Function, time: number, immediate: boolean = true) => {
let timeout;

return (...args) => {
const context = this;
if (immediate && !timeout) {
origFunc.apply(context, args);
}
clearTimeout(timeout);
timeout = setTimeout(() => {
origFunc.apply(this, args);
timeout = setTimeout(function () {
timeout = null;
if (!immediate) {
origFunc.apply(context, args);
}
}, time);
};
};

0 comments on commit ca8e2f2

Please sign in to comment.