我每次都尝试增加计数器,但是当单击 html 时,计数器会呈指数增长,如 +1、+2、+3、+4。
$('.dates').click(function(){
$('#output').html(function(i, val) { return val*1+1 });
});
HTML:
<div id="output">0</div>
心形动画代码:
var rand = Math.floor(Math.random() * 100 + 1);
var flows = ["flowOne", "flowTwo", "flowThree"];
var colors = ["colOne", "colTwo", "colThree", "colFour", "colFive", "colSix"];
var timing = (Math.random() * (1.3 - 0.3) + 1.6).toFixed(1);
// Animate Particle
$('<div class="particle part-' + rand + ' ' + colors[Math.floor(Math.random() * 6)] + '" style="font-size:' + Math.floor(Math.random() * (30 - 22) + 22) + 'px;"><i class="fa fa-heart-o"></i><i class="fa fa-heart"></i></div>').appendTo('.particle-box').css({ animation: "" + flows[Math.floor(Math.random() * 3)] + " " + timing + "s linear" });
$('.part-' + rand).show();
// Remove Particle
setTimeout(function () {
$('.part-' + rand).remove();
}, timing * 1000 - 100);
- 为什么它会呈指数级增长?
- 如何让它每次加1?比如0、1、2、3、4
最佳答案
很可能,您每次点击时都会注册一个新的点击事件监听器。
请检查并发布 $('.dates').click(...)
行周围的代码。
确保此行在代码中仅调用一次,以后不会再次调用。
关于javascript - 计数器按指数递增,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37547951/