Javascript简单的问题,在某一天显示div和调用函数

Javascript简单的问题,在某一天显示div和调用函数,javascript,Javascript,伙计们,我有个问题。我对javascript非常缺乏经验。我基本上希望在星期五显示一个div,并在星期五调用lightning()函数。是的,这是一个可怕的脚本,你可以猜到它要去哪里,但我不知道如何使用我当前的脚本调用这两个脚本 <script type="text/javascript"> <!-- //Lighting script var flash=0 function lightning() {flash=flash+1;

伙计们,我有个问题。我对javascript非常缺乏经验。我基本上希望在星期五显示一个div,并在星期五调用lightning()函数。是的,这是一个可怕的脚本,你可以猜到它要去哪里,但我不知道如何使用我当前的脚本调用这两个脚本

<script type="text/javascript">

    <!--
    //Lighting script
    var flash=0
    function lightning()
    {flash=flash+1;
    if(flash==1){document.bgColor='green'; setTimeout("lightning()",100);}
    if(flash==2){document.bgColor='black'; setTimeout("lightning()",90);}
    if(flash==3){document.bgColor='red'; setTimeout("lightning()",85);}
    if(flash==4){document.bgColor='blue'; setTimeout("lightning()",80);}
    if(flash==5){document.bgColor='purple'; setTimeout("lightning()",75);}
    if(flash==6){document.bgColor='green'; setTimeout("lightning()",70);}
    if(flash==7){document.bgColor='black'; setTimeout("lightning()",65);}
    if(flash==8){document.bgColor='red'; setTimeout("lightning()",60);}
    if(flash==9){document.bgColor='blue'; setTimeout("lightning()",50);}
    if(flash==10){document.bgColor='purple'; setTimeout("lightning()",40);}
    if(flash==11){document.bgColor='black'; setTimeout("lightning()",30);}
    if(flash==12){document.bgColor='red'; setTimeout("lightning()",25);}
    if(flash==13){document.bgColor='red'; setTimeout("lightning()",20);}
    if(flash==14){document.bgColor='blue'; setTimeout("lightning()",10);}
    if(flash==15){document.bgColor='purple'; setTimeout("lightning()",5);}
    if(flash==16){document.bgColor='white'; setTimeout("lightning()",1);}
    if(flash==17){document.bgColor='black'; setTimeout("lightning()",1);}
    if(flash==18){document.bgColor='blue'; setTimeout("lightning()",1);}
    if(flash==19){document.bgColor='purple'; setTimeout("lightning()",1);}
    if(flash==20){flash=0; setTimeout("lightning()",100);}
    }

    // -->

    onload=function(){
        var rightNow = new Date();
        var day = rightNow.getDay();
        var hour = rightNow.getHours();
        var minute = rightNow.getMinutes();
        var formDisplay = 'none'; // unless we see otherwise
        var forwardDisplay = 'block'; // unless we see otherwise

        if(day==1 || day==2  ||  day==3  ||  day==4 ) {  // friday friday, got to get down on Friday            if((hour>=1)  &&  (hour<=24)) // if chat is avalable between these times
                formDisplay = 'block', forwardDisplay = 'none'; 

            }

            }


    document.getElementById('friday').style.display = formDisplay; 
    document.getElementById('friday').style.display = forwardDisplay; 

    }

    // alert('Test Alert')
    </script>

    <div id='friday' >
    <object width="560" height="349"><param name="movie" value="http://www.youtube.com/v/CD2LRROpph0?fs=1&autoplay=1&amp;hl=en_US&amp;hd=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/CD2LRROpph0?fs=1&autoplay=1&amp;hl=en_US&amp;hd=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="560" height="349"></embed></object>
    </div>

onload=函数(){
var rightNow=新日期();
var day=rightNow.getDay();
var hour=rightNow.getHours();
var minute=rightNow.getMinutes();
var formDisplay='none';//除非我们看到其他情况
var forwardDisplay='block';//除非我们看到其他情况
如果(day==1 | | day==2 | | day==3 | | | day==4){///周五周五,如果((小时>=1)&&&(小时这是什么


你需要清理它,以便更容易地看到发生了什么。用这个替换你的onload函数

onload=function(){
  var rightNow = new Date();
  var day = rightNow.getDay();
  var isFriday = (day == 5);
  if(isFriday){
    document.getElementById('friday').style.display = 'block';
    lightning();
  } else {
    document.getElementById('friday').style.display = 'none';
  }
}

只是想发布如何清理此部件:

//Lighting script
var flash = 0;
var timeout = 0;
var color = '';

function lightning() {
    flash = flash + 1;

    if (flash === 1) { color = 'green'; timeout = 100; }
    else if (flash === 2) { color = 'black'; timeout = 90; }
    else if (flash === 3) { color = 'red'; timeout = 85; }
    else if (flash === 4) { color = 'blue'; timeout = 80; }
    else if (flash === 5) { color = 'purple'; timeout = 75; }
    else if (flash === 6) { color = 'green'; timeout = 70; }
    else if (flash === 7) { color = 'black'; timeout = 65; }
    else if (flash === 8) { color = 'red'; timeout = 60; }
    else if (flash === 9) { color = 'blue'; timeout = 50; }
    else if (flash === 10) { color = 'purple'; timeout = 40; }
    else if (flash === 11) { color = 'black'; timeout = 30; }
    else if (flash === 12) { color = 'red'; timeout = 25; }
    else if (flash === 13) { color = 'red'; timeout = 20; }
    else if (flash === 14) { color = 'blue'; timeout = 10; }
    else if (flash === 15) { color = 'purple'; timeout = 5; }
    else if (flash === 16) { color = 'white'; timeout = 1; }
    else if (flash === 17) { color = 'black'; timeout = 1; }
    else if (flash === 18) { color = 'blue'; timeout = 1; }
    else if (flash === 19) { color = 'purple'; timeout = 1; }
    else if (flash === 20) { flash = 0; timeout = 100; }

            document.bgColor = color;

    setTimeout(function() {
        lightning();
    }, timeout);
}
或使用开关/大小写语法:

//Lighting script
var flash = 0;
var timeout = 0;
var color = '';

function lightning() {
    flash = flash + 1;

    switch (flash) {
        case 1: color = 'green'; timeout = 100; break;
        case 2: color = 'black'; timeout = 90; break;
        case 3: color = 'red'; timeout = 85; break;
        case 4: color = 'blue'; timeout = 80; break;
        case 5: color = 'purple'; timeout = 75; break;
        case 6: color = 'green'; timeout = 70; break;
        case 7: color = 'black'; timeout = 65; break;
        case 8: color = 'red'; timeout = 60; break;
        case 9: color = 'blue'; timeout = 50; break;
        case 10: color = 'purple'; timeout = 40; break;
        case 11: color = 'black'; timeout = 30; break;
        case 12: color = 'red'; timeout = 25; break;
        case 13: color = 'red'; timeout = 20; break;
        case 14: color = 'blue'; timeout = 10; break;
        case 15: color = 'purple'; timeout = 5; break;
        case 16: color = 'white'; timeout = 1; break;
        case 17: color = 'black'; timeout = 1; break;
        case 18: color = 'blue'; timeout = 1; break;
        case 19: color = 'purple'; timeout = 1; break;
        case 20: flash = 0; timeout = 100; break;
    }   

    document.bgColor = color;

    setTimeout(function () {
        lightning();
    }, timeout);
}

是什么阻碍了你?它实际上是在计算是否是星期五,把页面放在一起吗?嗨,Jon,谢谢你的回答。我正在努力寻找一种方法,在星期五显示div并调用lightning函数。脚本正确地计算出是否是星期五。我不熟悉语法以及如何使用当前的脚本,我尝试的都不管用。天哪,那
lighting()
函数简直是噩梦。为什么你的javascript中有html注释?我认为你不需要Netscape 1的浏览器支持。它的运行看起来很漂亮。你帮我创建了一个怪物。感谢你的输入,这个片段似乎忽略了“今天是什么日子”,不管哪天都会触发那个恼人的小闪烁函数。我需要它只在第==5天启动。这很奇怪,这里有一个第==5天的检查。不管怎样,你似乎已经找到了一个解决方案:)迈克,非常感谢你。太好了;我只是对语法有点困惑。你是个好人。在这里使用switch语句可能更好。我本来打算使用switch,但是这个窗口中的滚动会很慢。去他妈的,反正我会把它放进去的。
//Lighting script
var flash = 0;
var timeout = 0;
var color = '';

function lightning() {
    flash = flash + 1;

    switch (flash) {
        case 1: color = 'green'; timeout = 100; break;
        case 2: color = 'black'; timeout = 90; break;
        case 3: color = 'red'; timeout = 85; break;
        case 4: color = 'blue'; timeout = 80; break;
        case 5: color = 'purple'; timeout = 75; break;
        case 6: color = 'green'; timeout = 70; break;
        case 7: color = 'black'; timeout = 65; break;
        case 8: color = 'red'; timeout = 60; break;
        case 9: color = 'blue'; timeout = 50; break;
        case 10: color = 'purple'; timeout = 40; break;
        case 11: color = 'black'; timeout = 30; break;
        case 12: color = 'red'; timeout = 25; break;
        case 13: color = 'red'; timeout = 20; break;
        case 14: color = 'blue'; timeout = 10; break;
        case 15: color = 'purple'; timeout = 5; break;
        case 16: color = 'white'; timeout = 1; break;
        case 17: color = 'black'; timeout = 1; break;
        case 18: color = 'blue'; timeout = 1; break;
        case 19: color = 'purple'; timeout = 1; break;
        case 20: flash = 0; timeout = 100; break;
    }   

    document.bgColor = color;

    setTimeout(function () {
        lightning();
    }, timeout);
}