<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分钟更新一次
	
	function updateAir() {
		var url = 'https://devapi.qweather.com/v7/air/now?location=101040600&key=1271def7ae8e49008a6f794eb30f2d87';
		$.getJSON(url, function(data) {
			var aqi = data.now.aqi;
			var level = data.now.level;
			var html = 'AQI:<span style='margin-left: 10px;'>' + aqi + '</span><span style='margin-left: 10px;'>' + level + '</span>';
			$('#air').html(html);
		});
	}
	
	updateAir();
	setInterval(updateAir, 600000); // 每10分钟更新一次
});
</script>
实时天气和空气质量展示代码:响应式更新、获取涪陵区数据

原文地址: https://www.cveoy.top/t/topic/oYYR 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录