【发布时间】:2017-02-19 11:46:49
【问题描述】:
我正在使用 jquery 和 json 数组在网站上显示当前的月相和日出/设置时间。这适用于日出/设置,但对于月相我遇到了问题。
当“closestphase”在请求数据的同一天时,其他 2 个字段被删除 - “currentphase”和“fracillum”。这意味着我不能每月显示 4 次相位或电流照明...
例如,下面的链接显示了 fracillum 和当前阶段: http://api.usno.navy.mil/rstt/oneday?date=today&coords=54.97N,1.61W&tz=0
但在此数据中被省略: http://api.usno.navy.mil/rstt/oneday?date=01/28/2017&coords=54.97N,1.61W&tz=0
我已经编写了下面的代码来说明如果 curphase 和 fracillum 未定义 并且 最接近的相位是“新月”或“满月”,则显示 fracillum 为“0% 或“100%”分别。
请有人能指出我做错了什么吗?
$(document).ready(function(){
$.get("http://api.usno.navy.mil/rstt/oneday?date=01/28/2017&coords=54.97N,1.61W&tz=0", function(data){
checkPhase(data);
});
});
$(document).ready(function(){
$("#suntimes").click(function(){
$("#sun").toggle("slide");
});
});
$(document).ready(function(){
$("#moontimes").click(function(){
$("#moon").toggle("slide");
});
});
$(document).ready(function(){
$("#Mobilesuntimes").click(function(){
$("#Mobilesun").slideToggle();
});
});
$(document).ready(function(){
$("#Mobilemoontimes").click(function(){
$("#Mobilemoon").slideToggle();
});
});
function checkPhase(data){
if(data.curphase == "undefined" && data.closestphase.phase == "New Moon"){data.fracillum == "0%";}
else if(data.curphase == "undefined" && data.closestphase.phase == "Full Moon")
{data.fracillum == "100%";}
$("#sun").append("<p><b>Newcastle, UK</b><br />Sunrise: " + data.sundata[1].time + " AM<br />Sunset: " + data.sundata[3].time + " PM</p>" );
$("#moon").append("<p><b>Newcastle, UK</b><br />Percentage Illuminated: " + data.fracillum + "<br />Moon Phase: " + data.curphase + "</p>" );
$("#Mobilesun").append("<p><b>Newcastle, UK</b><br />Sunrise: " + data.sundata[1].time + " AM<br />Sunset: " + data.sundata[3].time + " PM</p>" );
$("#Mobilemoon").append("<p><b>Newcastle, UK</b><br />Percentage Illuminated: " + data.fracillum + "<br />Moon Phase: " + data.curphase + "</p>"
);
}
【问题讨论】:
标签: javascript jquery html json api