Skip to content

Commit

Permalink
Merge pull request #324 from bjoernricks/throttle-helper
Browse files Browse the repository at this point in the history
Throttle helper
  • Loading branch information
swaterkamp committed Feb 1, 2018
2 parents 3cf853f + ee470e6 commit a11b50b
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions ng/src/gmp/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,16 @@ export function split(string, seperator, limit) {
return splits;
}

/**
* Group multiple sequential calls in a single one
*
* @param {Function} func Function to call
* @param {Number} wait Wait time until no more calls to the wrapper
* function are fired
* @param {Boolean} immediate Call func initially
*
* @returns {Function} Wrapper function
*/
export function debounce(func, wait, immediate = false) {
let timeout;
return function(...args) {
Expand All @@ -264,6 +274,30 @@ export function debounce(func, wait, immediate = false) {
};
}

/**
* Throttle animation paths by using requestAnimationFrame
*
* If a animation is scheduled func will not be called. Only
* after the animation has been rendered func can will be called again.
*
* @param {Function} func Function to call
*
* @returns {Function} Wrapper function
*/
export const throttleAnimation = func => {
let calling = false;
return (...args) => {

if (!calling) {
calling = true;
window.requestAnimationFrame(() => {
calling = false;
func(...args);
});
}
};
};

/**
* Calculate the sum of an Array
*
Expand Down

0 comments on commit a11b50b

Please sign in to comment.