admin管理员组

文章数量:1400026

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?

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?

Share Improve this question asked Dec 29, 2011 at 19:15 GV1GV1 4751 gold badge10 silver badges17 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

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.

You could delete the eventListener after the function was called the first time.

本文标签: javascriptDOMSubtreeModifiedHow to ensure it only fires onceStack Overflow