1
- console . log ( "main js" ) ;
2
-
3
1
const searchProduct = document . getElementById ( "search" ) ;
4
2
const matchItems = document . getElementById ( "match-items" ) ;
5
3
6
- const searchProductsItems = async ( search ) => {
4
+ const searchPeople = async ( search ) => {
7
5
const res = await fetch ( "../../data/data.json" ) ;
8
6
const products = await res . json ( ) ;
9
7
//get all the matched names from api
@@ -23,35 +21,37 @@ const searchProductsItems = async (search) => {
23
21
showPeopleDetails ( getMatches ) ;
24
22
} ;
25
23
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
+
26
36
const showPeopleDetails = ( matched ) => {
27
37
if ( matched . length > 0 ) {
28
38
const html = matched
29
39
. map ( ( items ) => {
30
40
return `<div class="card">
31
41
<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>
36
46
</div>` ;
37
47
} )
38
48
. join ( " " ) ;
39
49
matchItems . innerHTML = html ;
40
50
}
41
51
} ;
42
52
53
+ // Create the debounced version of searchProductsItems outside of the event listener.
54
+ const debouncedSearch = debounce ( searchPeople , 500 ) ;
43
55
searchProduct . addEventListener ( "input" , ( ) =>
44
- debounce ( searchProductsItems ( searchProduct . value ) , 500 )
56
+ debouncedSearch ( searchProduct . value )
45
57
) ;
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