-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.js
68 lines (56 loc) · 1.48 KB
/
search.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
import navbar from "./components/navbar.js";
let nav_container = document.getElementById("nav-container");
nav_container.innerHTML = navbar();
document.getElementById("text").addEventListener("input", function () {
debounce(main, 1000);
});
async function getData() {
try {
let name = document.getElementById("text").value;
// console.log(name)
let res = await fetch(
`https://www.themealdb.com/api/json/v1/1/search.php?s=${name}`
);
let data = await res.json();
// console.log(data)
return data.meals;
} catch (error) {
console.log(error);
}
}
getData();
async function main() {
let data = await getData();
if (data == undefined) {
return;
}
append(data);
}
function append(data) {
document.getElementById("search-items").textContent = "";
data.map((e) => {
let div = document.createElement("div");
div.setAttribute("id", "div");
let name = document.createElement("h3");
name.textContent = e.strMeal;
let image = document.createElement("img");
image.src = e.strMealThumb;
image.setAttribute("id", "img");
div.append(image, name);
document.getElementById("search-items").append(div);
});
}
let timer;
function debounce(func, delay) {
if (timer) {
clearTimeout(timer);
}
timer = setTimeout(function () {
let name = document.getElementById("text").value;
if (name == "") {
document.getElementById("search-items").textContent = "";
} else {
func();
}
}, delay);
}