You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
13 lines
717 B
13 lines
717 B
2 years ago
|
# defer和nextTick有什么区别
|
||
|
|
||
|
研读FineUI代码时,会发现有的地方用了`BI.defer`,有的地方用了`BI.nextTick`,有什么区别呢?
|
||
|
|
||
|
`BI.defer`源自lodash,官方描述是Defers invoking the func until the current call stack has cleared.,翻译一下就是在当前调用栈执行完毕之后调用.
|
||
|
|
||
|
看起来很高大上实际上底层实现就是`settimeout(fn,0)`
|
||
|
|
||
|
`BI.nextTick`的实现相对复杂,采用的是"能力检测"策略,优先使用`Promise.resolve()`,不支持的话使用`MutationObserver`,不支持的话`setImmediate`,还是不支持的话
|
||
|
那就只能`settimeout(fn,0)`了
|
||
|
|
||
|
由此可见,实际作用都是异步的延迟执行某段事务,使用`BI.nextTick`效率会更高一些
|