Solved

right click stop

Posted on 2014-02-21
3
181 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 54

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 54

Expert Comment

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

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delay Form Submission without touching existing code? 5 23
HTML5 frame 5 25
jqury 17 30
Html form and modal / img src -problem 3 9
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 will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

773 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