This repository has been archived on 2023-08-29. You can view files and clone it, but cannot push or open issues/pull-requests.
yutent
/
anot.js
Archived
1
0
Fork 0

精简nextTick

master
宇天 2020-08-19 15:05:04 +08:00
parent 02ccc30e23
commit d1f3cd7a10
1 changed files with 8 additions and 20 deletions

View File

@ -4,14 +4,7 @@ let Anot = function(el) {
} }
/*视浏览器情况采用最快的异步回调*/ /*视浏览器情况采用最快的异步回调*/
Anot.nextTick = new (function() { Anot.nextTick = (function() {
// jshint ignore:line
let tickImmediate = window.setImmediate
let tickObserver = window.MutationObserver
if (tickImmediate) {
return tickImmediate.bind(window)
}
let queue = [] let queue = []
function callback() { function callback() {
let n = queue.length let n = queue.length
@ -21,21 +14,16 @@ Anot.nextTick = new (function() {
queue = queue.slice(n) queue = queue.slice(n)
} }
if (tickObserver) { let node = document.createTextNode('<!-- -->')
let node = document.createTextNode('anot') new MutationObserver(callback).observe(node, { characterData: true })
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 bool = false
return function(fn) { return function(fn) {
setTimeout(fn, 4) queue.push(fn)
bool = !bool
node.data = bool
} }
})() // jshint ignore:line })()
/********************************************************************* /*********************************************************************
* Anot的静态方法定义区 * * Anot的静态方法定义区 *