开发者

Javascript simple problem, show div and call function on certain day

Guys I've got a problem. I'm very ill-experienced with javascript. I basically want to show a div on fridays and also call the lightning() function on Fridays. Yes it's a horrible script and you can guess where it's 开发者_开发百科going but I can't figure out how to call both using my current script.

<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>

Could anybody point me in the right direction? Thank you in advance!


This?

if(day===5 ) { //if Friday
    formDisplay = 'block';
    forwardDisplay = 'none'; 
    lightning();
}


You need to clean this up so it's easier to see what's going on. Replace your onload function with this.

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';
  }
}


Just wanted to post how this part could be cleaned up:

//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);
}

Or with switch/case syntax:

//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);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜