Last active
August 29, 2015 13:57
-
-
Save krambuhl/9578438 to your computer and use it in GitHub Desktop.
A set of underscore mixins for timer functions.
This file contains hidden or 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
_.mixin({ | |
bindDelay: function(func, context, wait) { | |
return _.delay(_.bind(func, context || this), wait, _.last(arguments, 3)); | |
}, | |
bindDefer: function(func, context) { | |
return _.defer(_.bind(func, context || this), _.last(arguments, 2)); | |
}, | |
repeat: function(func, interval) { | |
var args = _.last(arguments, 2); | |
return setInterval(_.bind(func, null, args), interval); | |
}, | |
repeatForDuration: function(func, interval, duration) { | |
duration = duration || 500; | |
interval = interval || (1000 / 60); | |
var args = _.last(arguments, 3), | |
timer = setInterval(_.bind(func, null, args), interval); | |
_.delay(function() { | |
if (duration % interval == 0) func.apply(null, args); | |
clearTimeout(timer); | |
}, duration); | |
return timer; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
repeatForDuration will repeat a function on an interval and stop after a defined duration.