Skip to content

Commit 2853ebd

Browse files
committed
debounce issue resolved & naming changed
1 parent 46b504f commit 2853ebd

File tree

2 files changed

+29
-20
lines changed

2 files changed

+29
-20
lines changed

vanilla-js-autocomplete/src/css/style.css

+9
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,13 @@ body {
7373
border: 1px solid red;
7474
min-height: 250px;
7575
border-radius: 6px;
76+
77+
img {
78+
width: 100%;
79+
max-height: 250px;
80+
min-height: 200px;
81+
object-fit: cover;
82+
border-radius: 6px;
83+
}
7684
}
85+
+20-20
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
console.log("main js");
2-
31
const searchProduct = document.getElementById("search");
42
const matchItems = document.getElementById("match-items");
53

6-
const searchProductsItems = async (search) => {
4+
const searchPeople = async (search) => {
75
const res = await fetch("../../data/data.json");
86
const products = await res.json();
97
//get all the matched names from api
@@ -23,35 +21,37 @@ const searchProductsItems = async (search) => {
2321
showPeopleDetails(getMatches);
2422
};
2523

24+
const debounce = (fn, delay) => {
25+
let timeId;
26+
return (...args) => {
27+
if (timeId) {
28+
clearTimeout(timeId);
29+
}
30+
timeId = setTimeout(() => {
31+
fn(...args);
32+
}, delay);
33+
};
34+
};
35+
2636
const showPeopleDetails = (matched) => {
2737
if (matched.length > 0) {
2838
const html = matched
2939
.map((items) => {
3040
return `<div class="card">
3141
<div class="card-body">
32-
<img src=${items.product_img}/>
33-
<p>${items.description}</p>
34-
<p>${items.name}</p>
35-
</div>
42+
<img src=${items.product_img}/>
43+
<p class="person-name">${items.name}</p>
44+
<p class="person-detail">${items.description}</p>
45+
</div>
3646
</div>`;
3747
})
3848
.join(" ");
3949
matchItems.innerHTML = html;
4050
}
4151
};
4252

53+
// Create the debounced version of searchProductsItems outside of the event listener.
54+
const debouncedSearch = debounce(searchPeople, 500);
4355
searchProduct.addEventListener("input", () =>
44-
debounce(searchProductsItems(searchProduct.value), 500)
56+
debouncedSearch(searchProduct.value)
4557
);
46-
47-
const debounce = (fn, delay) => {
48-
let timeId;
49-
return (...args) => {
50-
if (timeId) {
51-
clearInterval(timeId);
52-
timeId = setTimeout(() => {
53-
fn(...args);
54-
}, delay);
55-
}
56-
};
57-
};

0 commit comments

Comments
 (0)