您好我无法弄清楚为什么当直接传递给keyup事件时debounce函数按预期工作; 但如果我将它包装在匿名函数中,它就不起作用.
我有问题:http : //jsfiddle.net/6hg95/1/
编辑:添加了我尝试过的所有东西.
HTML
<input id='anonFunction'/>
<input id='noReturnAnonFunction'/>
<input id='exeDebouncedFunc'/>
<input id='function'/>
<div id='output'></div>
Run Code Online (Sandbox Code Playgroud)
JAVASCRIPT
$(document).ready(function(){
$('#anonFunction').on('keyup', function () {
return _.debounce(debounceIt, 500, false); //Why does this differ from #function
});
$('#noReturnAnonFunction').on('keyup', function () {
_.debounce(debounceIt, 500, false); //Not being executed
});
$('#exeDebouncedFunc').on('keyup', function () {
_.debounce(debounceIt, 500, false)(); //Executing the debounced function results in wrong behaviour
});
$('#function').on('keyup', _.debounce(debounceIt, 500, false)); //This is working.
});
function debounceIt(){
$('#output').append('debounced');
}
Run Code Online (Sandbox Code Playgroud)
anonFunction并且noReturnAnonFunction不会启动去抖功能; 但是最后一次function …
使用 React、react-final-form 和 lodash debounce 函数,我想验证用户名是否尚未使用(该字段使用的是 react-final-form)。
我在获取 debounce 函数以从提取请求返回已解决的承诺时遇到问题。
我提供了以下代码和框链接来演示我的问题:
请谁能告诉我为什么我的代码不起作用。
验证的入口点来自在验证属性中引用的 this.isNameUnique 调用
import React from "react";
import { Field, Form } from "react-final-form";
import { debounce } from "lodash";
class DemoForm extends React.Component {
getIsNameUnique = name => {
console.log("getIsNameUnique", name);
// fake fetch request
return async name => {
const asyncResponse = await new Promise(resolve =>
setTimeout(resolve({ name: true }), 1000)
);
console.log("async api response", asyncResponse);
return asyncResponse;
};
};
debounceCreativeName = name => …Run Code Online (Sandbox Code Playgroud)