UNPKG

ng-tasty

Version:

A lightweight, flexible, and tasty collection of reusable UI components for AngularJS.

31 lines (30 loc) 765 B
/** * @ngdoc service * @name ngTasty.service.throttle * @description * # throttle * Factory in ngTasty. */ angular.module('ngTasty.service.throttle', []) .factory('throttle', function ($timeout) { return function (fn, threshhold, scope) { threshhold = threshhold || 250; var last, promise; return function throttle () { var context = scope || this; var now = Date.now(), args = arguments; if (last && now < last + threshhold) { // hold on to it $timeout.cancel(promise); promise = $timeout(function throttleTimeout () { last = now; fn.apply(context, args); }, threshhold); } else { last = now; fn.apply(context, args); } }; }; });