1 Star 0 Fork 0

CPU驱动/vuedemo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
asyTick.js 3.04 KB
一键复制 编辑 原始数据 按行查看 历史
wenfei ji 提交于 2017-03-27 19:40 . ceshi
function isNative (Ctor) {
return /native code/.test(Ctor.toString())
}
var isIOS=false;
var noop=function(){}
/**
* Defer a task to execute it asynchronously.
*/
var nextTick = (function () {
var callbacks = [];
var pending = false;
var timerFunc;
function nextTickHandler () {
pending = false;
//对callbacks进行深复制
var copies = callbacks.slice(0);
callbacks.length = 0;
for (var i = 0; i < copies.length; i++) {
copies[i]();
}
}
// the nextTick behavior leverages the microtask queue, which can be accessed
// via either native Promise.then or MutationObserver.
// MutationObserver has wider support, however it is seriously bugged in
// UIWebView in iOS >= 9.3.3 when triggered in touch event handlers. It
// completely stops working after triggering a few times... so, if native
// Promise is available, we will use it:
/* istanbul ignore if */
if (typeof Promise !== 'undefined' && isNative(Promise)) {
var p = Promise.resolve();
var logError = function (err) { console.error(err); };
timerFunc = function () {
p.then(nextTickHandler).catch(logError);
// in problematic UIWebViews, Promise.then doesn't completely break, but
// it can get stuck in a weird state where callbacks are pushed into the
// microtask queue but the queue isn't being flushed, until the browser
// needs to do some other work, e.g. handle a timer. Therefore we can
// "force" the microtask queue to be flushed by adding an empty timer.
if (isIOS) { setTimeout(noop); }
};
} else if (typeof MutationObserver !== 'undefined' && (
isNative(MutationObserver) ||
// PhantomJS and iOS 7.x
MutationObserver.toString() === '[object MutationObserverConstructor]'
)) {
// use MutationObserver where native Promise is not available,
// e.g. PhantomJS IE11, iOS7, Android 4.4
var counter = 1;
var observer = new MutationObserver(nextTickHandler);
var textNode = document.createTextNode(String(counter));
observer.observe(textNode, {
characterData: true
});
timerFunc = function () {
counter = (counter + 1) % 2;
textNode.data = String(counter);
};
} else {
// fallback to setTimeout
/* istanbul ignore next */
timerFunc = function () {
setTimeout(nextTickHandler, 0);
};
}
debugger
return function queueNextTick (cb, ctx) {
var _resolve;
callbacks.push(function () {
debugger
if (cb) { cb.call(ctx); }
if (_resolve) {
console.log(0);
_resolve(ctx); }
});
if (!pending) {
pending = true;
timerFunc();
}
if (!cb && typeof Promise !== 'undefined') {
return new Promise(function (resolve) {
console.log("now")
_resolve = resolve;
})
}
}
})();
nextTick().then(()=>{
console.log("last");
})
var ss={
isIOS:"sss"
}
nextTick(function(){
console.log("1" +this.isIOS);
},ss)
nextTick(function(){
console.log("2" +this.isIOS);
},ss)
// nextTick(function(){
// alert("2");
// })
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/jiwenfie/vuedemo.git
git@gitee.com:jiwenfie/vuedemo.git
jiwenfie
vuedemo
vuedemo
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385