gpt4 book ai didi

javascript - 跟踪全局变量的所有自定义事件

转载 作者:行者123 更新时间:2023-11-29 10:08:03 25 4
gpt4 key购买 nike

我有一个全局变量,我们称它为 jsEvents,它作为项目中所有事件的调度程序。大约有 200 个监听器和大约相同数量的自定义事件,例如

$(jsEvents).on('customEvent', function(){
//doing something
})

现在我想调查一个事件和另一个事件之间到底发生了什么。所以我想记录每个事件发生的时间,这样我就可以检查顺序是否正确。

一种方法是在每个事件监听器中写入 console.log,但是否可以监听 any 事件?并打出它的名字?

有点像

$(jsEvents).on('*', function(){
//here I want to console.log event's name
})

最佳答案

为了调试,您可以覆盖 jQuery 事件对象的分派(dispatch)。例如这样的事情:

var temp = $.event.dispatch;

$.event.dispatch = function(event){
// the event that is passed is the native event, if you want the jquery
// event, you need the fixed event.
console.log(jQuery.event.fix( event ));
temp.call(this, event);
}

https://jsfiddle.net/8a530fjx/

关于javascript - 跟踪全局变量的所有自定义事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39336278/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com