JavaScript 实现实时更新天气信息
<script>
$(document).ready(function() {
function updateWeather() {
var url = 'https://geoapi.qweather.com/v2/city/lookup?location=fuling&adm=chongqing&key=1271def7ae8e49008a6f794eb30f2d87';
$.getJSON(url, function(data) {
var cityId = data.location[0].id;
var weatherUrl = 'https://devapi.qweather.com/v7/weather/now?location=' + cityId + '&key=1271def7ae8e49008a6f794eb30f2d87';
$.getJSON(weatherUrl, function(weatherData) {
var weatherDesc = weatherData.now.text;
var temp = weatherData.now.temp;
var html = '涪陵区<span style='margin-left: 10px;'>' + weatherDesc + '</span><span style='margin-left: 10px;'>' + temp + '℃</span>';
$('#weather').html(html);
});
});
}
updateWeather();
setInterval(updateWeather, 600000); // 每10分钟更新一次
});
</script>
原文地址: https://www.cveoy.top/t/topic/oYYQ 著作权归作者所有。请勿转载和采集!