在偶数侦听器块中绑定和取消绑定

bind and unbind within a even listener block

本文关键字:绑定 取消 侦听器      更新时间:2023-09-26
$('.arrow').click(function(e){
    run();
    $(this).off(e); // unbind 
    setTimeout(function(){ /*rebind back*/ }, 700);   
});

如何在区块本身内重新绑定回点击事件?我只能设法关闭()单击,但无法重新绑定回来。

命名你的回调:

$('.arrow').on("click", function cb(e){
    run();
    var $this = $(this)
    $this.off(e); // unbind 
    setTimeout(function(){ $this.on("click", cb); }, 700);   
});

或者像我说的那样去抖动它。 选择您喜欢的去抖动 IMPL,这里有下划线:

$('.arrow').on("click", _.debounce(run, 700));