I have the following search bar with
<div class="d-flex">
<input class="form-control me-2" type="search" placeholder="Search Products" aria-label="Search" id="searchProduct">
</div>
I would like to fire an onInput event whenever user types it will call an API.
const debounce = (fn, delay = 1000) => {
let timerId = null;
return (...args) => {
clearTimeout(timerId);
timerId = setTimeout(() => fn(...args), delay);
};
};
let controller = new AbortController();
let signal = controller.signal;
const fetchProducts = (value, signal) => {
axios
.get(`/FYP-ECOMMERCE/api/products?search=${value}`, {
signal, // Pass the AbortSignal to the request
})
.then((response) => {
return response.data;
})
.then((data) => {
console.log(data);
})
.catch((error) => {
if (error.name === "AbortError") {
// Handle the request abortion (e.g., ignore or log it)
console.log("Request was aborted");
} else {
// Handle other errors
console.error("An error occurred:", error);
}
});
};
const onInput = debounce(fetchProducts, 500);
const searchBar = document.getElementById("searchProduct");
searchBar.addEventListener("input", (e) => {
const term = e.target.value;
if (term !== "") {
controller.abort();
controller = new AbortController();
signal = controller.signal;
onInput(e.target.value, signal);
}
});
So my question how to combine debounce and abortcontroller , so that the previous request gets aborted.
Thank you
fethcProductsfn === create a new controller for each requestthis will give you control over the controller
onInputa general function, add controller as param so that if controller exists, the current request will be aborted and fetch again with a new controllerthe onInput clearly can be written in a better way, but for now, I think this should do