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:
Example
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.