javascript - 为什么我的功能会间歇性地失败?
问题描述
我需要一些关于这个脚本的帮助。无法找出为什么它有时会失败。它大部分时间都在工作,但有时它只是显示空白页。
这是脚本(在文件夹中,我有 3 个名为 1.jpg、2.jpg 和 3.jpg 的图像文件。:
function chgbg() {
var d = new Date();
var h = d.getHours();
var n = d.getDay();
var totalCount = 3;
var num = Math.ceil(Math.random() * totalCount);
/* Monday - Friday */
if (n >= 1 && n < 5 && h >= 23 && h < 6) {
document.body.style.backgroundColor = "#FFFFFF";
document.body.style.backgroundImage = 'url(gfx/weekdays/00-06/' + num + '.jpg)';
}
if (n >= 1 && n < 5 && h >= 6 && h < 11) {
document.body.style.backgroundColor = "#FFFFFF";
document.body.style.backgroundImage = 'url(gfx/weekdays/06-11/' + num + '.jpg)';
}
if (n >= 1 && n < 5 && h >= 11 && h < 14) {
document.body.style.backgroundColor = "#FFFFFF";
document.body.style.backgroundImage = 'url(gfx/weekdays/11-14/' + num + '.jpg)';
}
if (n >= 1 && n < 5 && h >= 14 && h < 18) {
document.body.style.backgroundColor = "#FFFFFF";
document.body.style.backgroundImage = 'url(gfx/weekdays/14-18/' + num + '.jpg)';
}
if (n >= 1 && n < 5 && h >= 18 && h < 23) {
document.body.style.backgroundColor = "#FFFFFF";
document.body.style.backgroundImage = 'url(gfx/weekdays/18-00/' + num + '.jpg)';
}
/* Monday - Friday */
解决方案
你的第一个条件是不可能的
if (n >= 1 && n < 5 && h >= 23 && h < 6)
h
不能同时 >=23 和 <6。
推荐阅读
- r - 无法在 R 中已在内存中的磁盘上写入大型栅格
- javascript - IE11 - 在 Angular 7 中使用 Google 图表会出现语法错误
- angular - Angular:扩展具有多个孩子的服务
- git - 使用 Git 合并二进制文件时,有没有办法让 Difftool 询问是否只合并其中一个文件?
- javascript - D3线由多段组成
- php - RatchetPHP - 客户端在收到来自服务器的消息之前立即与服务器断开连接
- c# - C# HttpClient 请求 HTTPS 异常错误
- android - Android Volley StringRequest 有时会提供空婴儿车
- javascript - 如何从一个网页播放音乐并从另一个网页停止
- python - pyplot,为什么不显示 x 轴?