Solved

right click stop

Posted on 2014-02-21
3
184 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 57

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 57

Expert Comment

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article discusses how to implement server side field validation and display customized error messages to the client.
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…

737 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