jashkenas/underscore — pull request #1628 by josephspens (2014-05-06 18:50:51+00:00)
Title: Add _.before() function to negate _.after()

I'm not sure if this PR belongs in this repository or the contributing repository, please let me know if I need to move it.

I couldn't find any simple way to limit the number of times a function runs using existing underscore functions. I came up with a non-underscore solution, and it turned out I reverse engineered something similar to the _.after() function.

Unlike _.after(), which only runs a function after it has been called n times, _.before() runs a function _until_ it has been called n times.

An example usage of this behavior is adding items to a list. I set up a function to add the items to the list, but I want to limit the number of times an item is added. An underscore alternative would be something like using _.after() and null the function after n calls.