首页 > 解决方案 > 为什么我的功能会间歇性地失败?

问题描述

我需要一些关于这个脚本的帮助。无法找出为什么它有时会失败。它大部分时间都在工作,但有时它只是显示空白页。

这是脚本(在文件夹中,我有 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 */

标签: javascript

解决方案


你的第一个条件是不可能的

if (n >= 1 && n < 5 && h >= 23 && h < 6)

h不能同时 >=23 和 <6。


推荐阅读