Created
August 14, 2020 14:52
-
-
Save newyellow/f1b633dd71658a7242c19f426ee29a04 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title></title> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> | |
</head> | |
<body> | |
<div> | |
<input type="button" value="print" onmousedown="printArray();" /> | |
<input type="button" value="print 復興區" onmousedown="printTargetTemp('復興區');" /> | |
<div id="buttonsArea"> | |
</div> | |
</div> | |
<script> | |
let namesArray = []; | |
let tempData = []; | |
$(window).on('load', function (){ | |
getWeatherData(); | |
}); | |
function getWeatherData () { | |
let apiUrl = "https://opendata.cwb.gov.tw/api/v1/rest/datastore/F-D0047-007?Authorization=CWB-15391347-353E-466D-8C41-3C1D7D3CF28B&format=JSON"; | |
$.get(apiUrl, handleWeatherData); | |
} | |
function handleWeatherData (data){ | |
let namesCount = data.records.locations[0].location.length; | |
// put name | |
for(let i=0; i< namesCount; i++) | |
{ | |
let currentLocationName = data.records.locations[0].location[i].locationName; | |
namesArray[i] = currentLocationName; | |
let newButton = $('<div>'); | |
newButton.html(currentLocationName); | |
newButton.attr('id', i); | |
newButton.on('click', function(){ | |
printTargetTemp(this.id); | |
}); | |
$('#buttonsArea').append(newButton); | |
} | |
for(let i=0; i< namesCount; i++) | |
{ | |
let currentLocation = data.records.locations[0].location[i]; | |
let dataCount = currentLocation.weatherElement[1].time.length; | |
let currentName = currentLocation.locationName; | |
console.log(currentLocation.locationName); | |
tempData[i] = []; | |
for(let t=0; t< dataCount; t++) | |
{ | |
let targetValue = currentLocation.weatherElement[1].time[t].elementValue[0].value; | |
tempData[i][t] = targetValue; | |
} | |
} | |
} | |
function printArray () { | |
console.log("array print:"); | |
console.log(tempData); | |
} | |
function printTargetTemp (targetLocationIndex) | |
{ | |
//console.log(targetLocationIndex); | |
console.log(tempData[targetLocationIndex]); | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment