]> BookStack Code Mirror - bookstack/blob - resources/assets/js/services/util.js
Removed jquery usage from page-display
[bookstack] / resources / assets / js / services / util.js
1
2
3 /**
4  * Returns a function, that, as long as it continues to be invoked, will not
5  * be triggered. The function will be called after it stops being called for
6  * N milliseconds. If `immediate` is passed, trigger the function on the
7  * leading edge, instead of the trailing.
8  * @attribution https://davidwalsh.name/javascript-debounce-function
9  * @param func
10  * @param wait
11  * @param immediate
12  * @returns {Function}
13  */
14 export function debounce(func, wait, immediate) {
15     let timeout;
16     return function() {
17         const context = this, args = arguments;
18         const later = function() {
19             timeout = null;
20             if (!immediate) func.apply(context, args);
21         };
22         const callNow = immediate && !timeout;
23         clearTimeout(timeout);
24         timeout = setTimeout(later, wait);
25         if (callNow) func.apply(context, args);
26     };
27 };