javascript - 如何使条件语句中的条件语句起作用?
问题描述
我正在为一个站点开发天气 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 。基本上,我是根据我提到的三个参数来构建背景图像的文件路径。让我知道这是否也不起作用。
解决方案
由于 OpenWeatherMap API 在 JSON 响应中包含weather.icon 和wheather.id 字段,您可以使用它们来相应地设置您的背景。它将简化允许使用“/img/${icon}.png”之类的路径访问图像的代码
推荐阅读
- python - 将 tsv 文件读取到 Pandas Dataframe 时出现 FileNotFoundError
- typescript - 如何明确访问类中的私有方法?
- mysql - 大数据集上Mysql查询的优化
- c# - 计算基本构造函数的值并在之后访问该值?
- javascript - 如何从 api 自动更新数据?
- javascript - BigQuery Storage gRPC-Web:创建读取会话时出现 404 错误
- python-3.x - “UnboundLocalError:分配前引用的局部变量'playbutton'”在我移除我的演员时发生
- android - 数据绑定:有条件的 onclick 调用
- ruby-on-rails - 有什么危害
* 在 CORS 配置中? - javascript - 查找字符串中的最大数 [jquery]