Last active
May 4, 2017 10:38
-
-
Save 5iDS/76be987b847c371f3a924e6f8f4bc879 to your computer and use it in GitHub Desktop.
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
var Scheduler = (function () { | |
var tasks = []; | |
var minimum = 10; | |
var timeoutVar = null; | |
var output = { | |
add: function (func, context, timer, once) { | |
var iTimer = parseInt(timer); | |
context = context && typeof context === 'object' ? context : null; | |
if(typeof func === 'function' && !isNaN(iTimer) && iTimer > 0) { | |
tasks.push([func, context, iTimer, iTimer * minimum, once]); | |
} | |
}, | |
remove: function (func, context) { | |
for(var i=0, l=tasks.length; i<l; i++) { | |
if(tasks[i][0] === func && (tasks[i][1] === context || tasks[i][1] == null)) { | |
tasks.splice(i, 1); | |
return; | |
} | |
} | |
}, | |
halt: function () { | |
if(timeoutVar) { | |
clearInterval(timeoutVar); | |
} | |
} | |
}; | |
var schedule = function () { | |
for(var i=0, l=tasks.length; i<l; i++) { | |
if(tasks[i] instanceof Array) { | |
tasks[i][3] -= minimum; | |
if(tasks[i][3] < 0) { | |
tasks[i][3] = tasks[i][2] * minimum; | |
tasks[i][0].apply(tasks[i][1]); | |
if(tasks[i][4]) { | |
tasks.splice(i, 1); | |
} | |
} | |
} | |
} | |
}; | |
timeoutVar = setInterval(schedule, minimum); | |
return output; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment