From d1f3cd7a10fb7b4b29acdba94a8510157a802e9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=87=E5=A4=A9?= Date: Wed, 19 Aug 2020 15:05:04 +0800 Subject: [PATCH] =?UTF-8?q?=E7=B2=BE=E7=AE=80nextTick?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/02-core.js | 28 ++++++++-------------------- 1 file changed, 8 insertions(+), 20 deletions(-) diff --git a/src/02-core.js b/src/02-core.js index e6fe735..4699b43 100644 --- a/src/02-core.js +++ b/src/02-core.js @@ -4,14 +4,7 @@ let Anot = function(el) { } /*视浏览器情况采用最快的异步回调*/ -Anot.nextTick = new (function() { - // jshint ignore:line - let tickImmediate = window.setImmediate - let tickObserver = window.MutationObserver - if (tickImmediate) { - return tickImmediate.bind(window) - } - +Anot.nextTick = (function() { let queue = [] function callback() { let n = queue.length @@ -21,21 +14,16 @@ Anot.nextTick = new (function() { queue = queue.slice(n) } - if (tickObserver) { - let node = document.createTextNode('anot') - new tickObserver(callback).observe(node, { characterData: true }) // jshint ignore:line - let bool = false - return function(fn) { - queue.push(fn) - bool = !bool - node.data = bool - } - } + let node = document.createTextNode('') + new MutationObserver(callback).observe(node, { characterData: true }) + let bool = false return function(fn) { - setTimeout(fn, 4) + queue.push(fn) + bool = !bool + node.data = bool } -})() // jshint ignore:line +})() /********************************************************************* * Anot的静态方法定义区 *