首页 > 解决方案 > 如何使条件语句中的条件语句起作用?

问题描述

我正在为一个站点开发天气 API,我希望背景照片与天气状况、温度和一天中的时间相关。但是,当我试图将各种条件置于其工作条件之内时,它并没有像我预期的那样运行。这是代码:

if (hour24 > 18 || hour24 < 6) {
    imagePath += 'images/weather/night';
    if (res.weather[0].main === 'Clear' && res.main.temp >= 50) {
        imagePath += 'night.png';
    }
} else if (hour24 >= 6 && hour24 < 11) {
    imagePath += 'images/weather/morning';
} else {
    imagePath += 'images/weather/morning';
}

此外,这里是回购的链接:https ://github.com/rise-and-shane93/adenaMaryKay 。基本上,我是根据我提到的三个参数来构建背景图像的文件路径。让我知道这是否也不起作用。

标签: javascript

解决方案


由于 OpenWeatherMap API 在 JSON 响应中包含weather.icon 和wheather.id 字段,您可以使用它们来相应地设置您的背景。它将简化允许使用“/img/${icon}.png”之类的路径访问图像的代码

有关更多信息,请参阅https://openweathermap.org/weather-conditions


推荐阅读