Solved

right click stop

Posted on 2014-02-21
3
183 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 56

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 56

Expert Comment

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

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying 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

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
This article discusses how to create an extensible mechanism for linked drop downs.
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…

762 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