-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
72 lines (58 loc) · 1.54 KB
/
script.js
File metadata and controls
72 lines (58 loc) · 1.54 KB
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
let container = document.getElementById("container");
let api_key = `240c5ccb3ce3bf4f257d0acb12a7daff`;
let url = `https://gnews.io/api/v4/top-headlines?token=${api_key}`;
let timerId;
function debounce(func, delay) {
if (timerId) {
clearTimeout(timerId);
}
timerId = setTimeout(function () {
func();
}, delay);
}
async function main() {
try {
let data = await searchInput();
if (!data) {
return false;
}
console.log(data)
displayData(data.data);
} catch (err) {
console.log(err);
}
}
async function searchInput(){
let query = document.getElementById('SearchBox').value;
let data = await axios.get(`https://gnews.io/api/v4/search?q=${query}&token=${api_key}`)
return data;
}
async function getData(data) {
try {
let data = await axios({
method: 'get',
url: url,
// responseType: 'stream'
})
console.log(data);
displayData(data.data);
} catch (err) {
console.log(err);
}
}
getData();
function displayData(data) {
document.getElementById('container').innerText='';
var arr = data.articles;
arr.forEach(function (article) {
let newsitems = document.createElement("div");
let img = document.createElement("img");
img.src = article.image;
let title = document.createElement("h4");
title.textContent = article.title;
let author = document.createElement("p");
author.textContent = article.author;
newsitems.append(img, author, title);
container.append(newsitems);
});
}