Introduction When users double-click DOM objects (such as buttons and links, etc.), it has always been a troublesome problem for user interaction. Fortunately, jQuery provides a pretty great solution. That is .one().
.one() What does this method do? It attaches an element event handler and can only run the event handler function once per element.
Parameters .one( events [, selector ] [, data ], handler(eventObject) )
events Type: String
• Specifies one or more events to be added to the element. Multiple events separated by spaces. Must be a valid event. Just like "click" and "keydown.myPlugin".
Selector parameters Parameter type: String
•The selector string is used to filter out the child elements of the selected element that can trigger the event
•If Pass null or omit, the event will be triggered when it reaches the selected element
Data Parameter type: any type
• The value of this parameter will be passed to when the event is triggered Event handler function
Event handler function Parameter type: function type
•Function that should be called when the event is triggered
•false is also allowed because it simply returns false; Short form of function
Example
$("#saveBttn").one("click", function () {
alert("This will be displayed only once.");
});
Or
$("body").one(" click", "#saveBttn", function () {
alert("This displays if #saveBttn is the first thing clicked in the body.");
}); The key to the above code is:
•When the code execution ends, clicking on the element with the id saveBtn will pop up a warning box
•Following clicks will have no effect
•This is equivalent to ==>
$("#saveBttn").on("click", function (event ) {
alert("This will be displayed only once.");
$(this).off(event);
});
In other words this It has the same effect as explicitly calling off() in the bound event handler
For more information, please click
jQuery .one()
Summary The method mentioned above is a new feature of jQuery 1.7, so if your element click event is triggered more than once, this may be a solution. What an amazing method, please contact me if you have any questions.