js插件不更新新的天气温度,而是返回[object object]
simpleweatherjs js plugin doesnt update new weather temp but instead returns [object object]
嗨,我已经尝试了一个插件从simpleweatherjs我跟着那里的样本,并试图采取地理位置和自动更新的样本,使地理位置自动更新几分钟后脚本工作良好,直到它达到间隔,我已经设置,而不是它给我[对象对象]作为结果
这是我的脚本
<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+'°'+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 + '°' + 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/
相关文章:
- 函数未在Object.keys或Object.getOwnPropertyNames下列出,但可以调用
- delete在Object上效率低下,但在DOM Element's的数据属性,与null out相比
- 有没有一种方法可以列出Ember.Object的所有绑定
- 如何取消object.prototypes javascript的一个函数
- 为什么是文档.旧版应用程序中的DOM-object.properties为null
- 将*.js文件的内容放入Object中
- Object.prototype using 'this'
- 使用Object.create()的角度服务继承
- 如何使用object.assign()从其他对象引用基本对象属性
- 循环的数组推入在Object容器中具有不同的值
- reducers在redux中得到Function not Object,what'it’他错了
- JSON.stringify和Object.keys在同一个对象上产生不同的结果
- 在使用object.create创建的对象中使用super
- Join架构验证:Join.object定义数组中的有效键
- jquery打印[object XMLDocument]而不是文件内容
- ExtJS 4 Object.prototype fail
- JavaScript-从对象数组中输出随机OBJECT
- 为什么“{}+1”在Chrome和Firefox中排名第一,而字符串'[object object]1'
- 什么是“;原型;通过JavaScript中的Object Literal Notation创建的对象的链接
- js插件不更新新的天气温度,而是返回[object object]