I'm using jquery and a json array to display the current moon phase and sunrise/set times on a website. This works fine for sunrise/set but for moon phases I've encountered a problem.
When the "closestphase" is on the same day that the data is requested 2 other fields are removed - "currentphase" and "fracillum". This means I can't display the phase or current illumination 4 times per month...
For example the link below shows fracillum and current phase: http://api.usno.navy.mil/rstt/oneday?date=today&coords=54.97N,1.61W&tz=0
but is omitted from this data: http://api.usno.navy.mil/rstt/oneday?date=01/28/2017&coords=54.97N,1.61W&tz=0
I've written the code below to say if curphase and fracillum is undefined and the clostestphase is "New Moon" or "Full Moon" then display fracillum of "0% or "100%" respectively.
Please can someone identify what I'm doing wrong?
$(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>"
);
}