Created
January 13, 2014 08:12
-
-
Save zackbloom/8396437 to your computer and use it in GitHub Desktop.
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
| queue = []; | |
| function send(data){ | |
| queue.push(data); | |
| setTimeout(flush); | |
| } | |
| function flush(){ | |
| if (queue.length){ | |
| actuallyMakeRequest(queue); | |
| queue = []; | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The setTimeout will push flush onto the top of the timing queue. Once the current event loop tick has finished (all the js code has been executed), it will run flush. It doesn't matter how often we set the timeout, as once it get's cleared, subsequent calls will just check the length and return.
This means that code like:
would only make one request (I'm assuming your api supports including multiple events in a single request).