Home  >  Article  >  Web Front-end  >  Registering events multiple times will cause an event to be triggered multiple times_javascript tips

Registering events multiple times will cause an event to be triggered multiple times_javascript tips

WBOY
WBOYOriginal
2016-05-16 17:25:551443browse

A JavaScript logic will automatically bind the function to the click event of the button, but this code will register the event repeatedly. This relies on the onNext function to be triggered multiple times when the button is clicked.

This is because JQuery does not internally determine whether to bind multiple times. It is suspected that it uses an array internally to save the event function, and does not remove duplicates.

The following code prevents this problem. The solution is simple.

Copy code The code is as follows:

if (!this.hasPageBarEvents) {
$ (".pNext").on("click", this, this.onNext);
this.hasPageBarEvents = true;
}

Be careful about this feature when using JQuery in the future .
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn