Skip to content

Commit

Permalink
Switched from slice to splice.
Browse files Browse the repository at this point in the history
  • Loading branch information
shaunpersad committed Feb 27, 2016
1 parent a4e5bb8 commit bcf9d77
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "throttled-queue",
"version": "1.0.0",
"version": "1.0.1",
"description": "Throttles arbitrary code to execute a maximum number of times per interval. Best for making throttled API requests.",
"main": "throttled-queue.js",
"directories": {
Expand Down
25 changes: 8 additions & 17 deletions throttled-queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
max_requests_per_interval = 1;
}

if (interval < 200) {
console.warn('An interval of less than 200ms can create performance issues.');
}

var queue = [];
var last_called = Date.now();

Expand All @@ -56,24 +60,11 @@
return;
}

var num_requests = 0;

/**
* Execute the callbacks.
*/
while (queue.length && num_requests < max_requests_per_interval) {

var callback = queue[num_requests++];
callback();
}
/**
* Push the called items off the queue.
*/
if (num_requests < queue.length) {
queue = queue.slice(num_requests, queue.length);
} else {
queue = [];
var callbacks = queue.splice(0, max_requests_per_interval);
for(var x = 0; x < callbacks.length; x++) {
callbacks[x]();
}

last_called = Date.now();
timeout = setTimeout(dequeue, interval);
};
Expand Down

0 comments on commit bcf9d77

Please sign in to comment.