Last active
December 12, 2015 06:28
-
-
Save girvan/4729118 to your computer and use it in GitHub Desktop.
javascript queue
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
/* | |
Example: | |
queue(function(){ click_it(); }, 1000); | |
queue(function(){ refresh_it(); }, 1800); | |
// it will run click_it() at 1sec, and run refresh_it() at 2.8sec | |
*/ | |
var _queue_runner; | |
var _queues = []; | |
function queue(func, sleep) | |
{ | |
_queues.push([func, sleep]); | |
} | |
function run_queue() | |
{ | |
if(_queues.length === 0) | |
{ | |
_queue_runner = setTimeout(run_queue, 1000); | |
return; | |
} | |
var run = _queues.shift(); | |
run[0](); | |
_queue_runner = setTimeout(run_queue, run[1]); | |
} | |
clearTimeout(_queue_runner); | |
run_queue(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment