Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DOMSubtreeModified ... How to ensure it only fires once?

Tags:

javascript

When using DOMSubtreeModified I get multiple, sometimes 30+ events firing. Is it possible to ensure that my function is only called once regardless of DOMSubtreeModified is fired?

like image 506
GV1 Avatar asked Dec 29 '11 19:12

GV1


1 Answers

The Mutation Events are performance hogs which can't really be tamed well. Therefore, they have been deprecated over a year ago and should be used only when really needed.

You could use the new and shiny Mutation Observers from DOM Level 4 (follow the links there!). Where DOMSubtreeModified fired a thousand times, MutationObserver fires only once with all the modifications contained and accessible.

Works for (as of 2012/06):

  • Chrome 18+ (prefixed, window.WebKitMutationObserver)
  • Firefox 14+ (unprefixed)
  • WebKit nightlies

Other than that ... some hackish solution with removing the listener and scheduling to add it again in some time in future (a second or two) might be worth exploring.

like image 91
Petr Janeček Avatar answered Oct 05 '22 20:10

Petr Janeček