-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
121 lines (99 loc) · 2.96 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
const searchWrapper = document.querySelector(".search_wrapper")
const inputBox = searchWrapper.querySelector("input")
const searchBtn = document.querySelector(".search-btn")
const lists = searchWrapper.querySelector(".lists")
inputBox.onkeyup = (e) => {
debounce(getData, 3000, e.target.value)()
}
searchBtn.onclick = (e) => {
throttle(handleSearch, 3000)()
}
let timeoutHandle;
let progress = 150
let throttleProgress = 150
let timeoutId;
let intervalId;
function getData(value) {
if (value !== "") {
let searchResults = []
searchResults = products.filter(res => {
return res.includes(value)
})
searchResults = searchResults.map(data => {
return data = '<li>' + data + '</li>'
})
lists.classList.add("active")
showList(searchResults)
} else {
lists.classList.remove("active")
}
let allList = lists.querySelectorAll('li')
for (let i = 0; i < allList.length; i++) {
allList[i].setAttribute("onmouseover", "hover(this)")
allList[i].setAttribute("onclick", "select(this)")
}
}
function handleSearch() {
document.querySelector(".snackbar").classList.add("snackbar-enabled")
setTimeout(() => {
document.querySelector(".snackbar").classList.remove("snackbar-enabled")
}, 500);
}
function debounce(fn, delay, ...args) {
return function () {
if (timeoutHandle) {
clearTimeout(timeoutHandle);
timeoutHandle = null;
clearInterval(timeoutId)
progress = 150
}
timeoutId = setInterval(() => {
progress = progress - 0.5
document.querySelector(".dbounce-bar").style.width = `${progress}px`
if (progress === 0) {
clearInterval(timeoutId)
}
}, 10);
timeoutHandle = setTimeout(() => {
fn(...args)
}, delay)
}
}
let flag = true;
function throttle(fn, delay) {
return function () {
if (flag) {
throttleProgress = 150
fn();
intervalId = setInterval(() => {
throttleProgress = throttleProgress - 0.5
document.querySelector(".throttle-bar").style.width = `${throttleProgress}px`
if (throttleProgress === 0) {
clearInterval(intervalId)
}
}, 10);
flag = false
setTimeout(() => {
flag = true
}, delay);
}
}
}
function hover(element) {
inputBox.value = ""
let elementValue = element.textContent;
inputBox.value = elementValue
}
function select(element) {
let elementValue = element.textContent;
inputBox.value = elementValue
lists.classList.remove("active")
}
function showList(list) {
if (list.length > 0) {
lists.innerHTML = list.join("")
} else {
let inputValue = inputBox.value
lists.innerHTML = '<li>' + inputValue + '</li>'
}
}