我想确保一段代码执行,并坚持执行。
我的思维过程是,禁用所有事件监听器,启动一个带有setTimeout的递归promise链,恢复事件监听器。
类似的东西:
((_do)=>{
// some additional things based on suggestion from @Yury
var NOOP = () => {}
var origEvents = [EventTarget.prototype.removeEventListener,
Function.prototype.call, Function.prototype.bind,
Function.prototype.apply]
var copyEvents = origEvents.slice();
origEvents.map((c)=>{ c = NOOP });
window.recurse = function recurse(){
function this_here(_do) {
if (_do) {
var all = document.getElementsByTagName("*");
for (var i=0, max=all.length; i < max; i++) {
all[i].__nativeREL__ = all[i].removeEventListener
all[i].removeEventListener = () => {}
// add this back later
}
}
}
function test() {
var some_test = true
if (some_test){
return Promise.resolve('done with stuff')
} else {
return Promise.resolve()
}
}
function delay(ms) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve()
}, ms);
});
}
return Promise.resolve(r)
.then(this_here)
.then(delay)
.then(test)
.then(recurse)
.catch(e => {console.error(e)})
}
recurse()
})(true)
如何确保此代码执行并继续执行?
基本上停止页面上的所有内容,直到代码片段另有说明为止?
一边想:也许我可以在事件冒泡时添加某种中间件?
相关: