gpt4 book ai didi

javascript - simpleweatherjs js 插件不更新新的天气温度而是返回 [object object]

转载 作者:行者123 更新时间:2023-11-30 17:25:35 26 4
gpt4 key购买 nike

您好,我试过来自 simpleweatherjs 的插件我跟随那里的样本并尝试获取 geolocationauto update示例来制作一个在几分钟后自动更新的地理定位脚本工作正常直到它达到我设置的时间间隔而不是它给我 [Object Object] 作为结果

这是我的脚本

<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="jquery.simpleWeather.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
navigator.geolocation.getCurrentPosition(function(position) {
getWeather(position.coords.latitude+','+position.coords.longitude); //load weather using your lat/lng coordinates

});

});
$(function(){
getWeather();
setInterval(getWeather, 6000);
});

function getWeather(location, woeid) {

$.simpleWeather({
location: location,
woeid: woeid,
unit: 'c',
success: function(weather) {
html = '<h2>'+weather.temp+'&deg;'+weather.units.temp+'</h2>';
html += '<ul><li>'+weather.city+', '+weather.region+'</li>';
html += '<li class="currently">'+weather.currently+'</li></ul>';

$("#weather").html(html);
},
error: function(error) {
$("#weather").html('<p>'+error+'</p>');
}
});
}

</script>
<div id="weather"></div>

如果我做错了,有人可以检查我的代码吗?还是我在这里遗漏了什么?请帮助我,这让我发疯了

最佳答案

您正在从 setInterval 函数调用 getWeather 函数,但该函数需要传递两个参数,而您没有。如果您按如下方式重新安排代码,它将起作用:

<script type="text/javascript">
$(document).ready(function() {
getWeather();
setInterval(getWeather, 6000);
});

function getWeather() {

navigator.geolocation.getCurrentPosition(function(position) {

var location = (position.coords.latitude + ',' + position.coords.longitude);
var woeid = undefined;

$.simpleWeather({
location: location,
woeid: woeid,
unit: 'c',
success: function(weather) {
html = '<h2>' + weather.temp + '&deg;' + weather.units.temp + '</h2>';
html += '<ul><li>' + weather.city + ', ' + weather.region + '</li>';
html += '<li class="currently">' + weather.currently + '</li></ul>';

$("#weather").html(html);
},
error: function(error) {
$("#weather").html('<p>' + error + '</p>');
}
});
});
}
</script>
<div id="weather"></div>

您可以转到此 JSFiddle 来验证这一点:http://jsfiddle.net/wdPA4/

关于javascript - simpleweatherjs js 插件不更新新的天气温度而是返回 [object object],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24364802/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com