JS

microTask

https://jakearchibald.com/2015/tasks-microtasks-queues-and-schedules/

  1. macroTask , such as setTimeout/setIntervalXMLHttpRequest/fetch
  2. microTask , such as Promise.prototype.then/MutationObserver
  3. eventTask , such as EventTarget.prototype.addEventListener/onClick

https://developer.mozilla.org/zh-CN/docs/Web/API/MutationObserver``

  1. // 选择需要观察变动的节点
  2. const targetNode = document.getElementById('some-id');
  3. // 观察器的配置(需要观察什么变动)
  4. const config = { attributes: true, childList: true, subtree: true };
  5. // 当观察到变动时执行的回调函数
  6. const callback = function(mutationsList, observer) {
  7. // Use traditional 'for loops' for IE 11
  8. for(let mutation of mutationsList) {
  9. if (mutation.type === 'childList') {
  10. console.log('A child node has been added or removed.');
  11. }
  12. else if (mutation.type === 'attributes') {
  13. console.log('The ' + mutation.attributeName + ' attribute was modified.');
  14. }
  15. }
  16. };
  17. // 创建一个观察器实例并传入回调函数
  18. const observer = new MutationObserver(callback);
  19. // 以上述配置开始观察目标节点
  20. observer.observe(targetNode, config);
  21. // 之后,可停止观察
  22. observer.disconnect();