Solved

right click stop

Posted on 2014-02-21
3
179 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 51

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 51

Expert Comment

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…
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…

757 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now