views:

60

answers:

1

How do I control only firing an event once?

Actually, a quick google appears to elude to the fact that .one helps..

+4  A: 

You can use jQuery's one method, which will subscribe to only the first occurrence of an event.
For example:

$('something').one('click', function(e) {
    alert('You will only see this once.');
});
SLaks
OK, awesome, just wondering, what is the 'e' between your parentheses?Also, does this function require two parameters? 1 for what you want to only run once, and the other for the function?
BOSS
`e` is the event formal parameter for the callback. In this case it is unused. The two parameters are the event type and function.
Matthew Flaschen
Awesome! That is really helpful, what is the difference between this and .bind? Is it that it doesn't need to be unbound by something like .unbind?
BOSS
The `one` method can take three parameters; see the documentation. http://api.jquery.com/one/
SLaks
As stated in the documentation, calling the `one` method is equivalent to calling `bind`, then calling `unbind` in the handler.
SLaks
Hey thanks! That helps a ton.
BOSS