2017-07-20 19:57:28 +03:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
/**
|
2017-10-03 13:03:19 +03:00
|
|
|
* Create a debouncing function wrapper to only call the target function after a certain
|
|
|
|
* amount of time has passed without it being called.
|
2017-07-20 19:57:28 +03:00
|
|
|
*
|
2017-10-03 13:03:19 +03:00
|
|
|
* @param {Function} func
|
|
|
|
* The function to debounce
|
|
|
|
* @param {number} wait
|
|
|
|
* The wait period
|
|
|
|
* @param {Object} scope
|
|
|
|
* The scope to use for func
|
|
|
|
* @return {Function} The debounced function
|
2017-07-20 19:57:28 +03:00
|
|
|
*/
|
2017-10-03 13:03:19 +03:00
|
|
|
exports.debounce = function (func, wait, scope) {
|
|
|
|
let timer = null;
|
2017-07-20 19:57:28 +03:00
|
|
|
|
2017-10-03 13:03:19 +03:00
|
|
|
return function () {
|
|
|
|
if (timer) {
|
|
|
|
clearTimeout(timer);
|
2017-07-20 19:57:28 +03:00
|
|
|
}
|
|
|
|
|
2017-10-03 13:03:19 +03:00
|
|
|
let args = arguments;
|
|
|
|
timer = setTimeout(function () {
|
|
|
|
timer = null;
|
|
|
|
func.apply(scope, args);
|
|
|
|
}, wait);
|
2017-07-20 19:57:28 +03:00
|
|
|
};
|
|
|
|
};
|