Skip to content

Commit 6bec772

Browse files
committed
Avoid binding events in duplicate
This is particularly important as a resize event will trigger a call to this (via resize -> _init() -> set() -> image load -> image load -> bindEvents()) and the 'hover' event type also triggers a 'mouseover' event having added an event listener for the same. This all means that each time a browser sends a resize event (which can happen on other events than a viewport width/height change) more event listeners are stacked, leading to significant CPU usage for no value.
1 parent 9a655bb commit 6bec772

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

lib/web/magnifier/magnifier.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,13 @@
401401
}
402402

403403
function bindEvents(eType, thumb) {
404+
var eventFlag = 'hasBoundEvent_' + eType;
405+
if (thumb[eventFlag]) {
406+
// Events are already bound, no need to bind in duplicate
407+
return;
408+
}
409+
thumb[eventFlag] = true;
410+
404411
switch (eType) {
405412
case 'hover':
406413
hoverEvents(thumb);

0 commit comments

Comments
 (0)