-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
217 lines (190 loc) · 11.4 KB
/
index.html
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<!Doctype html>
<html>
<head>
<title>Wheather</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<input type="text", id = "city-name">
<form id = "allUnits" style="display: inline;">
<input type="radio" name = "unit" value = "metric" onchange = "changeUnit(this)" checked>Celcius
<input type="radio" name = "unit" value= "imperial" onchange = "changeUnit(this)">Fahrenheit
</form>
<button onclick="getTemp(document.getElementById('city-name'))">Search</button>
<button onclick="getTemp(this)" value="cur_loc">Current Location Temp</button>
<br>
<div id = "temprature"></div>
<div id = "icon"></div>
</body>
<script>
function getTemp(city)
{
clearOutputs();//Each time this function is called, it is for a new query. So clear the outputs already displayed.
if(city.value == "")
return;
let apiCall;
let apiKey = "44283735812afd1f31a23ba0d72b42f9";
if(city.value == "cur_loc") {
if (navigator.geolocation)
navigator.geolocation.getCurrentPosition(showPosition, showError);
else
alert("Location Tracking is not supported by this browser.");
}
else
makeRequest(`https://api.openweathermap.org/data/2.5/weather?q=${city.value}&appid=${apiKey}`);
}
function showPosition(position)
{
let apiKey = "44283735812afd1f31a23ba0d72b42f9";
let lat = position.coords.latitude;
let lon = position.coords.longitude;
makeRequest(`https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&appid=${apiKey}`);//made the request here
}
function makeRequest(apiCall)
{
apiCall += "&units=" + $("input[name=unit]:checked", '#allUnits').val();//The jquery syntax gives us the value of the selected radio button
console.log(apiCall);
$.ajax(
{
url: apiCall,
method: 'GET',
success: (response) => {
document.getElementById("temprature").innerHTML = `${(response.main.temp).toFixed(1)}`;
document.getElementById("icon").innerHTML = `${response.weather[0].description}`;
document.getElementById("city-name").value = response.name;
/*return response.name; /*
QUESTION -
how can I can return a value from here and it will be available to the caller of makeRequest()
[like showPosition()]
NOTE - This is not as simple as it seems!! You can't return a value in any normal way you think.
ANSWER -
given in the end comments.
*/
},
error: (response) => {
if(response.responseJSON.message === "city not found")
alert(response.responseJSON.message);
else
alert("Could not get the temp right now. Try again later");
},
}
);
}
function changeUnit(unit)
{
let tempdiv = document.getElementById("temprature");
let temp = tempdiv.innerHTML;
if(temp == "")
return;
else {
if(unit.value == "metric") //means we have to convert to celcius
tempdiv.innerHTML = ((temp - 32) * (5/9)).toFixed(1);
else //means we have to convert to fahrenheit
tempdiv.innerHTML = ((temp * (9/5)) + 32).toFixed(1);
}
}
function showError(error)
{
switch(error.code) {
case error.PERMISSION_DENIED:
alert("You denied to share your Location!!!.");
break;
case error.POSITION_UNAVAILABLE:
alert("Location information is unavailable.");
break;
case error.TIMEOUT:
alert("The request to get location timed out.");
break;
case error.UNKNOWN_ERROR:
alert("An unknown error occurred.");
break;
}
}
function clearOutputs() {
document.getElementById("temprature").innerHTML = "";
document.getElementById("icon").innerHTML = "";
}
</script>
</html>
<!--
ANSWER FOR THE QUESTION (REGARDING returning value from inside the sucess callback function) -
The appraches you tried -
1. First you explicitly tried to return from the sucess callback function itself as shown in the comment.
Reason for Failure - the callback function will return the value from the ajax() call, not from makeRequest(). And as ajax() is predefined
we can't make it return the value to makeRequest()
2. You made a variable (let's say "name") inside the makeRequest(). You assigned "name" the return value, inside the sucess callback function.
Now you returned it at the end of makeRequest(). Code is given below -
function makeRequest(apiCall, myResolve)
{
let name;
apiCall += "&units=" + $("input[name=unit]:checked", '#allUnits').val();//The jquery syntax gives us the value of the selected radio button
console.log(apiCall);
$.ajax(
{
url: apiCall,
method: 'GET',
success: (response) => {
document.getElementById("temprature").innerHTML = `${(response.main.temp).toFixed(1)}`;
document.getElementById("icon").innerHTML = `${response.weather[0].description}`;
name = response.name;
console.log(name);//this log will execute after we get response from server. Whihc will take some time. So first, the log given at the end will execute
},
error: (response) => {
if(response.responseJSON.message === "city not found")
alert(response.responseJSON.message);
else
alert("Could not get the temp right now. Try again later");
},
}
);
console.log(name);//first this will execute
return name
}
Reason for failure - the above did not work because ajax() works asynchronously.See when ajax() will be called, the request will be sent to server
which would take some time. So should we stop the program there? No, it would be better if let the program run untill we get the respose from server.
That is why ajax() is made asynchronous by its makers, it will not stop the program untill we get the response back, instead it
will transfer the control to code which is after it. The code after ajax() is our console.log() & return statement. Now the value of name will still
be undefined because it will be initialized only after we get the reponse from the server (inside the success callback function).
So the value returned will be undefined.
You can check this by seeing the logs produced by this code. You will get undefined first then the name of the city.
Solution -
The solution is to the call makeRequest() inside a promise and resolve it inside the callback function of success.
in the resolve() pass the value you want to return. This will make the return value available inside the then handler
of makeRequest() where you can do whatever you with the value.The solution code is given below -
NOTE - as we will be calling resolve() inside the makeRequest() we need to pass resolves' refrence as an argument to makeRequest() for callback.
function showPosition(position)
{
let apiKey = "44283735812afd1f31a23ba0d72b42f9";
let lat = position.coords.latitude;
let lon = position.coords.longitude;
new Promise(
(myResolve) => {
makeRequest(`https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&appid=${apiKey}`, myResolve);
}
).then(
(value) => { console.log("inside parentPromise:" +value); }
);
}
function makeRequest(apiCall, myResolve)
{
apiCall += "&units=" + $("input[name=unit]:checked", '#allUnits').val();//The jquery syntax gives us the value of the selected radio button
console.log(apiCall);
$.ajax(
{
url: apiCall,
method: 'GET',
success: (response) => {
document.getElementById("temprature").innerHTML = `${(response.main.temp).toFixed(1)}`;
document.getElementById("icon").innerHTML = `${response.weather[0].description}`;
myResolve(response.name);
},
error: (response) => {
if(response.responseJSON.message === "city not found")
alert(response.responseJSON.message);
else
alert("Could not get the temp right now. Try again later");
},
}
);
}
-->