jQuery中的Debounce功能 [英] Debounce function in jQuery

查看:760
本文介绍了jQuery中的Debounce功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Ben Alman的jquery debouncing库去除按钮的输入。
http://benalman.com/code/projects/jquery-throttle-debounce / examples / debounce /

I'm attempting to debounce a button's input using the jquery debouncing library by Ben Alman. http://benalman.com/code/projects/jquery-throttle-debounce/examples/debounce/

目前这是我的代码。

function foo() {
    console.log("It works!")
};

$(".my-btn").click(function() {
    $.debounce(250, foo);
});

问题是当我点击按钮时,该功能永远不会执行。我不确定我是否误解了一些东西,但据我所知,我的代码与示例匹配。

The problem is that when I click the button, the function never executes. I'm not sure if I've misunderstood something but as far as I can tell, my code matches the example.

推荐答案

我遇到了同样的问题。这个问题正在发生,因为debounce函数会返回一个新函数,而这个函数在任何地方都没有被调用。

I ran into the same issue. The problem is happening because the debounce function returns a new function which isn't being called anywhere.

要解决这个问题,你必须将debouncing函数传递给jquery click事件的参数。这是您应该拥有的代码。

To fix this, you will have to pass in the debouncing function as a parameter to the jquery click event. Here is the code that you should have.

$(".my-btn").click($.debounce(250, function(e) {
    console.log("It works!");
}));

这篇关于jQuery中的Debounce功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆