Powerful DOM change observer MutationObserver and mutationobserver
Before that, DOM3 provided the Mutation events event
You can listen to attributes, text content, node insertion and deletion, child node changes, and other events. However, the W3C event is obsolete. Although some browsers still support this event, it is not recommended.
MutationObserver currently
differs from events in that all listening operations and processing are performed asynchronously after the execution of other scripts, and so after the change is triggered, will become recorded in the array, unified callback, that is, when you use observer to listen to multiple DOM changes, and the number of DOM has changed, then Observer will change the changes in the array, waiting to end together, The corresponding callback function is then executed once from the array of changes.Mutation Ob
to indicate whether the callback function is being executed
TimerFunc: used to trigger the execution of the callback function
Next, let's take a look at the nextTickHandler () function.
function nextTickHandler () { pending = false const copies = callbacks.slice(0) callbacks.length = 0 for (let i = 0; i
This function is used to execute all the callback functions stored in callbacks.
Next, assign the trigger method to timerFunc.
First, determine whether the native supports promise. If ye
If the propertychange event is not supported, use this event or higher-end mutationobserver.
VaR root = document.doc umentelement supportdomattrmodified = root. addeventlistener? (Function () {var FN, F = false, id = root. ID; fn = function () {root. removeeventlistener ('domattrmodified', FN, false); supportdomattrmodified = true; root. id = ID; // restore it}; root. addeventlistener ('domattrmodified', FN, false); root. id = 'mass'; // update attr
Yesterday I tried a series of suspicious module interception (http://www.bkjia.com/Article/201406/310932.html) experiments, although the final solution still has some compatibility problems, but the general idea has been clear:
Static module: Use MutationObserver for scanning.
Dynamic module: blocks path attributes through API hooks.
When talking about Hook programs, you will think of API hooks in traditional applications and various plug-in Troja
Yesterday, I tried a series of suspicious module interception tests. Although the final scheme still has some compatibility problems, the general idea has been clarified:
Static module: Use mutationobserver for scanning.
Dynamic module: blocks path attributes through API hooks.
When talking about Hook programs, you will think of API hooks in traditional applications and various plug-in Trojans. Of course, it may not be a system function. Any CPU i
For example, I have a P element, inside is some text, and then I have a button, click the button to replace the contents of the P element, then the P element binding what event can let it tell me that it has been modified, what is the implementation method?
Reply content:
For example, I have a P element, inside is some text, and then I have a button, click the button to replace the contents of the P element, then the P element binding what event can let it tell me that it has been modified,
For example, I have a p element, which contains some text, and then I have a button, click the button to replace the content in the p element, so what kind of events can be bound to element p to notify me that it has been modified? is there any implementation method? For example, I have a p element, which contains some text, and then I have a button, click the button to replace the content in the p element, so what kind of events can be bound to element p to notify me that it has been modified?
: PUBSUBJS.Dirty CheckIn data binding: the model to the end of the view, the force dirty check two-way binding example. In general, the framework will automatically enter the $degist phase, but in some special cases, the user can also be done manually by means of the framework exposure, as regular provides the $update method.MutationobserverThe exciting time has come:
MutationObserverProvides developers with the ability to respond appropriately when a DOM tree changes in a range.
A
Native JS dynamically listens for DOM changes, binds different processing logic according to different typesFirefox and Chrome are prefixed with previous versionsvar mutationobserver =Window. Mutationobserver | |Window. Webkitmutationobserver | |Window. MozmutationobserverSelect Target Nodevar target =Document.queryselector (' #some-id ');To create an observer objectvar observer =New
are always lagging behind the reality, the rest of the standard browser has its own abacus, there are inconsistencies between them.1. The new events are very unstable , and there may be widespread obsolescence, and in the early days of the sizzle selector engine, there were a few sentences. false ); Document.addeventlistener (false); Document.addeventlistener (false);Now that the three events have been scrapped (and, to be exact, all the changes are over), FF14 and Chrome18 began using
of the current task, such as reacting to some actions or performing tasks asynchronously without assigning a new task, which can improve performance. as long as no other JS code in the execution stack is executing, and the macro task currently being transferred is executed, the Micro task queue executes immediately . If the micro task queue joins a new micro task during the micro-task execution, the new micro-task is added to the tail of the queue and then executed.Simply understood, the macro
Warm tip: This article is currently only available in Chrome 51 and above.
2016.11.1 additional, Firefox 52 has also been implemented.
2016.11.29 added, Firefox people worry about the current specification is not stable, the future is difficult to ensure backward compatibility, so disable the API, need to manually open the DOM. Intersectionobserver.enabled is only fine.
The Intersectionobserver API is used to monitor whether an element has scrolled into the viewable area of the browser window
First, JavaScript running steps// 基本概念1、代码块:一个Second, javascript operating mechanismJS engine will generate two queues: Macrotask queue andMicrotask QueueCreation of Task
macrotasks:script, setTimeout, setInterval, setImmediate, I/O, UI rendering
microtasks:process.nextTick, Promise, Object.observe, MutationObserver
JavaScript engine operating mechanism:
The default is to start execution from the first task in the Macrotask
expected timer is as follows:
The Code comes from developers.
Data Binding and DOM operations are performed by NX.
Developer-defined event hooks
Draw in the browser
Step 1
The NX register object runs synchronously Based on the ES6 proxy and DOM changes based on MutationObserver (Change observer) (details in the next section ). It acts as a microtask and is delayed until step 2 is executed. This latency has been converted to objects in Promise. resolv
JavaScript - varA = react.createelement (' Li ',NULL, ' A '); - varB = react.createelement (' ul ',NULL, react.createelement (' Li ',NULL, ' B ')); the varroot = React.createelement (' ul ',NULL, A, b); - //output The virtual DOM structure -Console.log (root);Open the console and we can see the output of a JavaScript object, yes that's what we call the virtual DOM object;Next we look at the diff algorithm in Reactjs, first we use the browser mutationobderver function, the page elements to liste
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.