开发者

I made a strobe light, how can I make it stop?

I made this strobe light function and the strobe function worked. I've been trying to add functionality for resetting the website and stopping the strobe. I tried this code but it hasn't worked. I've been trying to debug this for a while but can't think of anything. If you have any ideas of how to make this work they'd be greatly appreciated.

var text1=document.body.style.backgroundColor;
var text2=document.body.style.color;
var boolean_value1=true;
var boolean_value2=true;
function strobe()
{
 if (boolean_value2)
 {
  document.body.style.backgroundColor = boolean_value1 ? 'black' : 'white';
  document.body.style.color = boolean_value1 ? 'white' : 'black';
  boolean_value1=!boolean_value1;
  setTimeout(strobe, 70); //in milliseconds
 }
 else
 {
  document.body.style.backgroundColor=text1;
  document.body.style.color=text2;
  alert("The else statement was executed...boolean_value2 evaluates to false");
 }
}

function stop_strobe()
{
 boolean_value2=false;
}

I think as of now the main problem is that boolean_value2 doesn't evaluate to true. I can't understand why though. Isn't it a global variable? Yes, I have been calling these functions rather than just declaring them. But that's in a different file that I know works....this one:

<html>

<head>
 <title>Blag!</title>
 <script type="t开发者_C百科ext/javascript" src="/JAVASCRIPT/flash.js"></script>
 <script type="text/javascript" src="/JAVASCRIPT/konami.js"></script>
</head>



<body>
    <script type="text/javascript"> 
    // <![CDATA[

        var konami = new Konami();
        konami.pattern = "38384040373937396665";
        var counter=0;
        konami.code = function() {
            if ((counter%2)==0)
            {
             alert("You have entered the Konami code...enter it again to make it stop...")
             strobe();
             counter=counter+1;
            }
            else
            {
             stop_strobe();
            }
        };
        konami.load();

    // ]]>
    </script> 

</body>


When I test your code, it works just fine.

Demo: http://jsfiddle.net/R43ky/


You can use the setInterval method instead of setTimeout. That way you don't have to set a new timeout from the callback method, and you can easily stop it:

var text1 = document.body.style.backgroundColor;
var text2 = document.body.style.color;
var boolean_value1 = true;
var interval;

function strobe() {
  document.body.style.backgroundColor = boolean_value1 ? 'black' : 'white';
  document.body.style.color = boolean_value1 ? 'white' : 'black';
  boolean_value1 = !boolean_value1;
}

function start_strobe() {
  interval = window.setInterval(strobe, 70); //in milliseconds
}

function stop_strobe() {
  window.clearInterval(interval);
  document.body.style.backgroundColor = text1;
  document.body.style.color = text2;
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜