-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapiCalls.js
More file actions
51 lines (46 loc) · 1.7 KB
/
apiCalls.js
File metadata and controls
51 lines (46 loc) · 1.7 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
const API_ENDPOINT = "https://cors-proxy.dominic-fd8.workers.dev"; // Update this dynamically if needed
// Global boolean flag
let newImageFlag = false;
let firstBoot = true
// Function to check if a new image is available (using promises)
function checkNewImage() {
fetch(`${API_ENDPOINT}/new-image`)
.then(response => response.json())
.then(data => {
newImageFlag = data.newImageAvailable;
})
.catch(error => console.error("Error fetching image data:", error));
}
function sendTrueBool() {
fetch(`${API_ENDPOINT}/image-got`, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({ imageReceived: true })
})
.catch(error => console.error("Error sending data:", error));
}
// Function to fetch data from the server (using promises)
function fetchData() {
fetch(`${API_ENDPOINT}/get`)
.then(response => response.json())
.then(data => {
if (window.updateMap) {
window.updateMap(data.coordinates, data.label1Text, data.label2Text, data.zoomLevel);
// Only update image if newImageFlag is true
if (newImageFlag || firstBoot) {
window.updateImage(`${API_ENDPOINT}${data.imageUrl}`);
sendTrueBool();
firstBoot = false;
}
} else {
console.error("window.updateMap is not defined");
}
})
.catch(error => console.error("Error fetching data:", error));
}
// Poll the server every 5 seconds by checking new image flag and fetching data
setInterval(function () {
fetchData();
}, 5000);