Question

If any of the following listeners are activated, they are activated 2^x times, x being the number of times any one of them has been triggered. The first time it will run 2 times, ten 4, 8, 16 ect. What am I missing that is triggering them more than once?

$(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() {
});
}));
Was it helpful?

Solution

Presumably, either myRides.js or friends.js or both are adding duplicate event listeners every time you load it.

The logical question to ask would be why are you loading the same script over and over again? You probably should not be doing that.

If there is one function in that script that you want to execute, you can test whether that function is already loaded and if so, just call it. If not, then load the script and let it execute when it's loaded.

If there's some other reason why you are loading the same script over and over again, then you could protect each event listener from installing a subsequent copy by keeping track of whether you've loaded each one yet in it's own state variable, though this is probably the more tedious way to solve the problem.

OTHER TIPS

You are adding new handlers each time you "getScript"

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top