Created
June 18, 2013 11:23
-
-
Save vladimir-ivanov/5804601 to your computer and use it in GitHub Desktop.
AngularJs timer with throttle plus valid jshinting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*jshint bitwise:true, camelcase:true, curly:true, eqeqeq:true, forin:true, latedef:true, newcap:true, noarg:true, | |
noempty:true, nonew:true, undef:true, unused:true, strict:true, browser:true, camelcase:false, unused: false */ | |
/*globals | |
$: false, | |
app: false, | |
angular: false, | |
factory: false, | |
UrlHelper: false, | |
showFeedUrl: false, | |
formatDate: false, | |
_: false | |
*/ | |
var TimerFactory = function ($rootScope) { | |
"use strict"; | |
var DEFAULT_TIMEOUT = 1000; | |
var timers = {}; | |
return { | |
throttle: function (callback, timeout) { | |
if (_.isEmpty(callback.name)) { | |
throw new Error('The callback requires a name'); | |
} | |
timeout = timeout || DEFAULT_TIMEOUT; | |
clearTimeout(timers[callback.name]); | |
timers[callback.name] = setTimeout(function () { | |
callback(); | |
}, timeout); | |
} | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment