简体   繁体   English

javascript jQuery回调累积

[英]javascript jquery callback accumulates

Issue: Each time div.a is clicked, the number of callbacks when div.b is clicked,accumulates by 1. 问题:每次单击div.a时,单击div.b时的回调数累计为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; 单击div.a一次,然后单击div.b => alert()弹出;
if i click div.a again and click div.b => alert() pops-up twice consecutively; 如果我再次单击div.a并单击div.b => alert()连续两次弹出;
if i click div.a another time and click div.b => alert() pops-up three times consecutively; 如果我再次单击div.a并单击div.b => alert()连续弹出三遍;
..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. 我不知道问题的原因是什么,或者仅仅是我对JS中的回调函数的误解/滥用。 Any insights and/or advice will be greatly appreciated. 任何见解和/或建议将不胜感激。 thanks 谢谢

Quickest fix for your code without a refactor is to just unbind and re-bind. 无需重构,对代码的最快修复就是取消绑定并重新绑定。

var $=jQuery;
$(function(){
   $("div.a").click(function(){cbf(trgmsg);});
});
function trgmsg(){
   alert($.now());
}
function cbf(cb){
   $("div.b").off('click').click(function(){cb()});
}

What you have now (refactored) looks like: 您现在拥有的(重构)如下所示:

(function($){
  $(function(){
    function trgmsg(){
      alert($.now());
    }
    $('div.a').click(function(){
      // every time `div.a` is clicked a new click event is
      // attached to `div.b`.
      $('div.b').click(function(){ // maybe use `.one()` instead of `.click()`?
        cb();
      });
    });
  });
})(jQuery);

You may want to use .one() so after it binds it's only executed once. 您可能要使用.one()因此在绑定后仅执行一次。 Or else you need to elaborate on what the actual goal is and see if that can be solves differently. 否则,您需要详细说明实际目标是什么,看看是否可以通过其他方式解决。

If you just want to call the supplied function against div.b , when div.a is clicked, this might work for you 如果您只想针对div.b调用提供的函数,则在单击div.b时,这可能对您div.a

var $=jQuery;
$(function(){
   $("div.a").click(function(){cbf(trgmsg);});
});
function trgmsg(){
   alert($.now());
}
function cbf(cb){
   $("div.b")[0].apply(cb);
}

You are attaching multiple click events onto div.b. 您要将多个点击事件附加到div.b上。 So every time you click on div.a it is attaching another click event onto div.b so when div.b is clicked each of it's click events are fired. 因此,每次单击div.a时,它都会将另一个click事件附加到div.b上,因此当div.b被单击时,都会触发其每个click事件。 You can try setting a flag that says a click has already been registered. 您可以尝试设置一个标志,表明已经注册了一次点击。

var $=jQuery;
var flag = 0;
$(function(){
   $("div.a").click(function(){cbf(trgmsg);});
});
function trgmsg(){
   alert($.now());
}
function cbf(cb){
   if (!flag) {
       flag = 1;
       $("div.b").click(function(){cb()});
   }
}

Although I'm not quite sure what your using this for so that may not be your desired functionality. 尽管我不太确定您为此使用了什么功能,但这可能不是您想要的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM