我已将angular2集成到主要使用JS / Jquery的现有项目中。现有的JS代码绑定诸如“mousemove”,“mousedown”等事件,使得每当用户点击或移动页面中的任何地方时,事件被触发&重置计时器以及调用回调函数(下面的伪代码)
body.bind("mousemove keydown mousewheel mousedown", function () {
if (timer) {
clearTimeout(timer); //reset timer function
}
// Set a new timer.
timer = setTimeout(some_callback_function, 5000)
})
问题是每次触发任何事件时,也会触发angular2区域功能(对于完整代码,请参阅url => https://unpkg.com/zone.js@0.7.4)。有没有办法跳过angular2区域功能&事件有问题吗?换句话说,我们可以手动调用angular2区域函数作为&需要时。
function ZoneTask(type, zone, source, callback, options, scheduleFn, cancelFn) {
this.runCount = 0;
this.type = type;
this.zone = zone;
this.source = source;
this.data = options;
this.scheduleFn = scheduleFn;
this.cancelFn = cancelFn;
this.callback = callback;
var self = this;
this.invoke = function () {
_numberOfNestedTaskFrames++;
try {
return zone.runTask(self, this, arguments);
}
finally {
if (_numberOfNestedTaskFrames == 1) {
drainMicroTaskQueue();
}
_numberOfNestedTaskFrames--;
}
};
}
PS: - 从堆栈跟踪中我可以看到绑定是在'patchTimer'函数中在同一'zone.js'文件中完成的,该文件执行行var task = zone.scheduleMacroTask(setName, args[0], options, scheduleTask, clearTask);
并调用function ZoneTask