'use strict';
angular.module('mgcrea.ngStrap.helpers.debounce', [])
// @source jashkenas/underscore
// @url https://github.com/jashkenas/underscore/blob/1.5.2/underscore.js#L693
.factory('debounce', function ($timeout) {
return function (func, wait, immediate) {
var timeout = null;
return function () {
var context = this;
var args = arguments;
var callNow = immediate && !timeout;
if (timeout) {
$timeout.cancel(timeout);
}
timeout = $timeout(function later() {
timeout = null;
Eif (!immediate) {
func.apply(context, args);
}
}, wait, false);
Iif (callNow) {
func.apply(context, args);
}
return timeout;
};
};
})
// @source jashkenas/underscore
// @url https://github.com/jashkenas/underscore/blob/1.5.2/underscore.js#L661
.factory('throttle', function ($timeout) {
return function (func, wait, options) {
var timeout = null;
Eif (!options) options = {};
return function () {
var context = this;
var args = arguments;
if (!timeout) {
Eif (options.leading !== false) {
func.apply(context, args);
}
timeout = $timeout(function later() {
timeout = null;
Eif (options.trailing !== false) {
func.apply(context, args);
}
}, wait, false);
}
};
};
});
|