-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcounter.js
73 lines (64 loc) · 2.18 KB
/
counter.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
// Hornbills Sighted
let counterPlusElem = document.getElementById('counter-plus');
let counterDisplayElem = document.getElementById('counter-display');
let counterReset = document.getElementById('reset');
let target = document.getElementById('recent-hornbill-spotted');
let content = counterDisplayElem.innerHTML;
let count = 0;
if(localStorage.getItem("count") != null){
count = localStorage.getItem("count");
}
let date = [];
if(localStorage.getItem("date") != null){
date = JSON.parse(localStorage.getItem("date"));
}
let loc = [];
if(localStorage.getItem("loc") != null){
loc = JSON.parse(localStorage.getItem("loc"));
}
updateDisplay();
counterPlusElem.addEventListener("click",()=>{
count++;
updateLocalStorage();
updateDisplay();
localStorage.setItem("count", count);
});
counterReset.addEventListener("click",()=>{
count = 0;
localStorage.setItem("count", count);
date = [];
localStorage.setItem("date", JSON.stringify(date));
loc = [];
localStorage.setItem("loc", JSON.stringify(loc));
updateDisplay();
});
function updateDisplay(){
counterDisplayElem.innerHTML = content + count.toString();
if(loc.length == 0 || date.length == 0){
let response = ""
target.innerHTML = response;
}
else{
let response = "Most Recent Hornbill Spotted at "+ loc[loc.length-1] + "\nat " + date[date.length-1];
target.innerHTML = response;
}
};
// Updates Geolocation in Local Storage
function updateLocalStorage() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
console.error("Geolocation is not supported by this browser.");
}
}
function showPosition(position) {
date.push(Date());
loc.push("(" + position.coords.latitude + "," + position.coords.longitude + ")");
// After we identify the position, we will add localStorage.
localStorage.setItem("date", JSON.stringify(date));
localStorage.setItem("loc", JSON.stringify(loc));
let response = "Most Recent Hornbill Spotted at "+"(" + position.coords.latitude + ","
+ position.coords.longitude + ")" + "\nat " + Date();
console.log(response)
target.innerHTML = response;
}