控制台记录器在一个mousedown上调用了两次

时间:2015-05-30 12:19:47

标签: jquery plugins onmousedown

我遇到了一个奇怪的问题,jQuery的onmousedown。我有一个插件,您可以为其分配按钮并添加一些选项,如下所示:

$.lollipopbutton(".buttons button", {
    bubble_color: "rgba(0,0,0,1)"
});

此插件中发生的一件事是,当您单击按钮时,将触发动画。但是,我只想让这个动画一次触发x次。因此,我添加了选项maxClicks(默认值= 10用于测试目的)。此外,每个按钮都有一个jQuery数据元素来跟踪计数器:

counter = $this.data("counter") || 0;

然后我们可以将计数器与maxClicks选项进行比较:

if (counter <= args.maxClicks) {}

并将我们只希望一次执行两次的函数放在那里:

        if (counter <= args.maxClicks) {
            // Do stuff
            $this.data("counter", ++counter);
            console.log(counter);

            setTimeout(function () {
                $this.children(".bubble:first").remove();
                if (!$this.children(".bubble").length) {
                    $this.data("counter", --counter);
                }
            }, totalTimeout);
        }
    });

但奇怪的是,console.log()每次点击返回两次,总是两个后续数字(例如1&amp; 2,3&amp; 4,5&amp; 6等等)。

我尝试了一切,但我无法隔离问题。我在这里错过了什么?插件(wip)可以找到here in a fiddle

1 个答案:

答案 0 :(得分:0)

它被调用两次,因为在lollipopbutton函数中你附加了一个mousedown处理程序。你打电话给lollipopbutton就好了

$.lollipopbutton(".buttons button", {
    bubble_color: "rgba(0,0,0,1)"
});

$.lollipopbutton(".buttons button.white", {
    text_overlay: false
});

这将附加mousedown处理程序两次

相关问题