如果激活了以下监听器中的任何一个,它们都会被激活2^x次,其中x是其中任何一个被触发的次数。第一次它将运行2次,10次4,8,16等等。我错过了什么会不止一次触发它们?
$(document).on('click',"#post-ride",(function() {
addRide(currentDriver, $(destinationInput).val(), $(originInput).val(),$(dateInput).val(), $(timeInput).val());
console.log("add ride called");
$.getScript("scripts/myRides.js", function() {
});
}));
$(document).on('click',"#request-ride",(function() {
requestRide(currentDriver, $(destinationInput).val(), $(originInput).val(), $(dateInput).val(), $(timeInput).val());
$.getScript("scripts/myRides.js", function() {
});
}));
$(document).on('click',"#leave-ride",(function() {
leaveRide(currentDriver, $(this).closest('div').attr('id') );
$.getScript("scripts/myRides.js", function() {
});
console.log("leave ride called");
}));
$(document).on('click',"#cancel-ride",(function() {
cancelRide(currentDriver, $(this).closest('div').attr('id') );
$.getScript("scripts/myRides.js", function() {
});
}));
$(document).on('click',"#remove-friend",(function() {
removeFriend(currentDriver, $(this).closest('div').attr('id') );
$.getScript("scripts/friends.js", function() {
});
}));
$(document).on('click',"#add-friend",(function() {
addFriend(currentDriver, $(this).closest('div').attr('id') );
$.getScript("scripts/friends.js", function() {
});
}));
发布于 2012-12-04 08:07:31
据推测,每次加载时,myRides.js
和/或friends.js
都会添加重复的事件侦听器。
要问的逻辑问题是,为什么要一遍又一遍地加载相同的脚本?你可能不应该这么做。
如果脚本中有一个您想要执行函数,则可以测试该函数是否已加载,如果已加载,则只需调用它。如果没有,那么加载脚本,并让它在加载时执行。
如果您一次又一次地加载相同的脚本有其他原因,那么您可以通过跟踪是否在每个事件侦听器的状态变量中加载了每个事件侦听器,来防止其安装后续副本,尽管这可能是解决问题的更繁琐的方法。
发布于 2012-12-04 08:05:21
您每次"getScript“时都会添加新的处理程序。
https://stackoverflow.com/questions/13693973
复制相似问题