Created
August 25, 2014 12:19
-
-
Save lsauer/cf70e65c208cc311ce97 to your computer and use it in GitHub Desktop.
custom-scoped-setInterval-JavaScript.js: intervalCall binds a function to a declared scope and invokes it after 'tm' milliseconds
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
//lsauer.com, 2012 lo sauer | |
//description: intervalCall binds a function to a declared scope and invokes it after 'tm' milliseconds | |
//@param scope: scope in which the function should be invoked | |
//@param fn: the function to be invoked | |
//@param tm: timeout in milliseconds | |
//[@param fntimes]: Optional. A function to control the number of invocations of 'fn', i.e. to declare events / triggers based on the invocation-count | |
// fntimes is passed an integer Number-counter, starting at 1; | |
function intervalCall(scope, fn, tm, fntimes) | |
{ | |
var _fn = (function(){ | |
var _self = scope, _fnset = fn, _cnt = 1, _fntimes = fntimes; | |
return function(){ _fnset.call(_self); _fntimes && _fntimes.call(_self, _cnt++)} | |
}).call(scope||this); | |
return setInterval( _fn, tm); | |
} | |
//Examples: | |
//Example #1 | |
new intervalCall(document, function(){console.log("test", this)}, 200); | |
//Example #2 | |
var _ifn = doIntervalntimes( function(i){ console.log(i); if(i >= 5){clearInterval(_ifn); } }, function(){console.log("test", this)}, 500, document); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment