Issue: Each time div.a is clicked, the number of callbacks when div.b is clicked,accumulates by 1.
var $=jQuery;
$(function(){
$("div.a").click(function(){cbf(trgmsg);});
});
function trgmsg(){
alert($.now());
}
function cbf(cb){
$("div.b").click(function(){cb()});
}
Result:
click div.a once and click div.b => alert() pops-up;
if i click div.a again and click div.b => alert() pops-up twice consecutively;
if i click div.a another time and click div.b => alert() pops-up three times consecutively;
..and so on
I have No idea what the cause of the problem is or if its simply my misunderstanding/misuse of callback functions in JS. Any insights and/or advice will be greatly appreciated. thanks
What you have now (refactored) looks like:
You may want to use
.one()
so after it binds it's only executed once. Or else you need to elaborate on what the actual goal is and see if that can be solves differently.