gpt4 book ai didi

javascript - 从事 fetch/axios/debounce 项目

转载 作者:行者123 更新时间:2023-12-02 21:00:06 26 4
gpt4 key购买 nike

我刚刚完成了由名为 Colt Steele/Steven Grider 的讲师在 Udemy 上开设的在线类(class)的大部分教程,并面临项目困惑。

我正在参与一个项目,其中有人介绍我制作一个“防抖”功能,以便每次我在输入栏中输入按键时都不会执行 API 调用。但是,我不理解 debounce 函数背后的逻辑(例如,返回后的传播(...args)以及它提供的总体“屏蔽”。我想知道是否有人可以帮助以更清晰的方式解释这一点?谢谢!

const fetchData = async (searchTerm) =>{ //mark the function as async
const response = await axios.get('http://www.omdbapi.com/',{ //await to get response from async, calls the web portal
params: {
apikey: 'xxx',
s: 'avengers'
},
});
console.log(response.data)
};

const input = document.querySelector('input');

const debounce = (func) => {
let timeoutId;
return (...args) => {
if(timeoutId){
clearTimeout(timeoutId);
}
timeoutId = setTimeout(()=>{
func.apply(null, args);
},1000)
};
};

const onInput = event =>{
fetchData(event.target.value);
}

input.addEventListener('input', debounce(onInput))

最佳答案

如果您想在codesanbox here is a link中看到它,我将逐步解释您的代码。 .

const input = document.querySelector("input");

/*High Order Function
a higher-order function is a function that does at least one of the following:

* takes one or more functions as arguments (i.e. procedural parameters),
* returns a function as its result.

Closure
Here you will see info about that js topic
https://levelup.gitconnected.com/using-closures-in-javascript-to-create-private-variables-c0b358757fe0
*/

// this is a high order function
const debounce = func => {
/* this variable save the setTimeout id and the reason why it
will be persist is because this is the this is a closure and for that,
the returned function will remember their execution context
and therefore the value of timeoutId and calledNTimes*/
let timeoutId;
let calledNTimes = 0;

/* this args parameter comes from the function passed as
argument to debounce, and their value is the event from the addEventListener
function, you can call args as you like but normaly you will
find it with this name.


you use the spread operator to the argument from addEventListener as an
array, because func.apply accept an array of args */
return (...args) => {
console.log((calledNTimes += 1), timeoutId);
console.log("args", args);

/* After the first debounce called, the setTimeout will be
clear while the user is typing, or until it is executed */
if (timeoutId) {
clearTimeout(timeoutId);
}
timeoutId = setTimeout(() => {
/* with func apply method you execute the function with
the args comming from the original call, and this args
value is the event from the addEventListener */

func.apply(null, args);
}, 1000);
};
};

const onInput = event => {
fetchData(event.target.value);
};

/* some interesting thing is that as you are executing debounce here
(because you add the parentesis), you can see the second parameter of
addEventListener as the anonymous function returned by debounce, I mean
to say:

(...args) => {
if (timeoutId) {
clearTimeout(timeoutId);
}
timeoutId = setTimeout(() => {
func.apply(null, args);
}, 1000);

but if you pass literally the function, you will lose the closure feature.
*/

input.addEventListener("input", debounce(onInput));

/* if you console.log the params with and without the spread operator you will
see the difference.*/
function testFunction(...params) {
console.log(params);
}

input.addEventListener("input", testFunction);

关于javascript - 从事 fetch/axios/debounce 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61376813/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com