# Backbone.JS once() Event

The Backbone.JS event once() method extends the Backbone.model class while a backbone model is being created. It works the same as the Backbone.JS event on() method with the only difference that it causes the firing of the bound callback only once and after that it is removed.

Syntax:

object.once(event, callback, context)


Parameters:
event: This parameter is used to specify the event whose binding with an object is needed.
callback: This parameter is used to specify the callback function that will be executed when an event is fired.
context: This is an optional parameter which is used to specify an object to be used to pass to a callback function.

Example:

<!DOCTYPE html> <html> <head> <title>Example</title> <script src="https://code.jquery.com/jquery-2.1.3.min.js" type="text/javascript"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js" type="text/javascript"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js" type="text/javascript"></script> </head> <body> <script type="text/javascript"> var X = _.extend({msg:'Hello World! This is an example for Event once() method.'}, Backbone.Events); X.once('func', function () { document.write("The Triggered message for you is: "); document.write(this.msg); }); X.trigger('func'); </script> </body> </html>

Output:

The Triggered message for you is: Hello World! This is an example for Event once() method.

Explanation:
The Event once() method will bind the event to an object. Exactly in a similar way as the Event On() method, the callback function will then be executed when triggered by the object and thus the message will be displayed. But the Event once() method unlike the Event On() method will fire the callback() function only once before being removed.