Solved

right click stop

Posted on 2014-02-21
3
182 Views
Last Modified: 2014-02-21
Hello there,
I would like to know if its possible to have a start/stop based on right click..
right now what I have is it changed to black and white every 1.5 second automatically but I would like it to start based on right click.. how can I do that?

<!DOCTYPE html>
<html>
    <head>
        <script type="text/javascript">

            function change_color(flag){
                var color = null;
                if (flag === true){
                    var color = "black";
                }else{
                    var color = "white";
                }
                document.body.style.background = color;
                flag = !flag
                var t=setTimeout(function(){change_color(flag)},1500);
            }
        </script>
    </head>

    <body onload="change_color(true)">
</body>
</html>

Open in new window

0
Comment
Question by:XK8ER
  • 2
3 Comments
 
LVL 55

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 39877145
How about this
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
// VARIABLE TO MAINTAIN STATE
var flashing = false;
var color = 'white';

// CAPTURE THE RIGHT MOUSE CLICK
document.oncontextmenu = function() {

  // IF WE ARE ALREADY FLASHING THEN STOP THE FLASHING
  if (flashing) {
    // CLEAR THE TIMEOUT
    clearInterval(flashing);
	
	// SET FLASHING TO FALSE TO TOGGLE FUNCTIONALITY
    flashing = false;
  }
  else {
    // OTHERWISE START THE FLASHING
    flashing = setInterval(change_color, 1500);
  }
  return true;
}

function change_color(){
  // SET BACKGROUND COLOUR TO CURRENT COLOUR
  document.body.style.background = color;
  
  // TOGGLE COLOUR
  color = (color == 'white') ? 'black' : 'white';
}
</script>
</head>

<body>
</body>
</html>

Open in new window

0
 
LVL 1

Author Closing Comment

by:XK8ER
ID: 39877169
beautiful work of art
0
 
LVL 55

Expert Comment

by:Julian Hansen
ID: 39877492
You are welcome - thanks for the points and the compliment.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question