精简nextTick
parent
02ccc30e23
commit
d1f3cd7a10
|
@ -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
|
let bool = false
|
||||||
return function(fn) {
|
return function(fn) {
|
||||||
queue.push(fn)
|
queue.push(fn)
|
||||||
bool = !bool
|
bool = !bool
|
||||||
node.data = bool
|
node.data = bool
|
||||||
}
|
}
|
||||||
}
|
})()
|
||||||
|
|
||||||
return function(fn) {
|
|
||||||
setTimeout(fn, 4)
|
|
||||||
}
|
|
||||||
})() // jshint ignore:line
|
|
||||||
|
|
||||||
/*********************************************************************
|
/*********************************************************************
|
||||||
* Anot的静态方法定义区 *
|
* Anot的静态方法定义区 *
|
||||||
|
|
Reference in New Issue