Scroll to see replies
var lightstates = {red:0,amber:1,green:2};
var currentState = lightstates.green;
var onClick= function(){
changeState();
}
function changeState(){
switch(currentState){
case lightstates.red{
currentState = lightStates.amber
break;
case lightstates.amber:{
currentState = lightStates.red;
}
}
}
}
var lightstates = {red:0,amber:1,green:2};
var currentState = lightstates.green;
var onClick= function(){
changeState();
}
function changeState(){
switch(currentState){
case lightstates.red{
currentState = lighgStates.green;
break;
case lightstates.amber:{
currentState = lightStates.red;
}
}
}
}
<div class="traffic-light">
<div class="light off" id="red"></div>
<div class="light off" id="amber"></div>
<div class="light off" id="green"></div>
</div>
<button id="changeBtn">Change</button>
.traffic-light
{
width:50px;
height:75px;
background-color:gray;
}
.light
{
width:20px;
height:20px;
border:1px solid;
margin-left:auto;
margin-right:auto;
border-radius: 50px;
}
.red
{
background-color:red
}
.amber
{
background-color:yellow
}
.green
{
background-color:green;
}
.off
{
background-color:transparent;
}
Javascript
var lightStates = {red:0,amber:1,green:2};
var currentState = lightStates.red;
document.getElementById('changeBtn'.onclick=function(){
changeState();
};
function changeState()
{
clear();
switch(currentState)
{
case lightStates.red:
{
document.getElementById("red".className ="light red";
currentState = lightStates.amber;
}
break;
case lightStates.amber:
{
document.getElementById("amber".className ="light amber";
currentState = lightStates.green;
} break;
case lightStates.green:
{
document.getElementById("green".className ="light green";
currentState = lightStates.red;
} break;
}
}
function clear(){
document.getElementById("red".className ="light off";
document.getElementById("amber".className ="light off";
document.getElementById("green".className ="light off";
}
You can play with it here:window.onload = setInterval(changeState, 1000);
window.onload = setInterval(changeState, 1000);
window.onload = setInterval(changeState, 1000);
<link rel="stylesheet" type="text/css" href="new 2.css">
<script src="new 3.js">
<head>
<!-- you put your title here etc, remember to remove spaces in between file names -->
<link rel="stylesheet" type="text/css" href="new2.css">
<script type="text/javascript" src="new3.js">
</head>
Last reply 9 minutes ago
Woodhouse College applicants 2024Last reply 15 minutes ago
Official London School of Economics and Political Science 2024 Applicant ThreadLast reply 17 minutes ago
AQA GCSE English Literature Paper 1 (8702/1) - 13th May 2024 [Exam Chat]Last reply 20 minutes ago
Kingston or Westminster university for architecture?Last reply 24 minutes ago
Official: King's College London A100 2024 Entry Applicants1020
Last reply 1 week ago
OCR A-level Computer Science Paper 2 (H446/02) - 19th June 2023 [Exam Chat]Last reply 2 weeks ago
AQA A-level Computer Science Paper 1 (7517 1A - 1E) - 12th June 2023 [Exam Chat]Last reply 4 weeks ago
OCR A-level Computer Science Paper 1 (H446/01) - 12th June 2023 [Exam Chat]Last reply 2 months ago
OCR GCSE Computer Science Paper 1 (J277/01) - 19th May 2023 [Exam Chat]Last reply 3 months ago
AQA GCSE Computer Science Paper 2 (8525/2) - 25th May 2023 [Exam Chat]Last reply 5 months ago
AQA GCSE Computer Science Paper 1 (8525/1A, 1B, 1C) - 19th May 2023 [Exam Chat]Last reply 5 months ago
AQA Computer Science NEA and Computer science paper 1 questionLast reply 1 week ago
OCR A-level Computer Science Paper 2 (H446/02) - 19th June 2023 [Exam Chat]Last reply 2 weeks ago
AQA A-level Computer Science Paper 1 (7517 1A - 1E) - 12th June 2023 [Exam Chat]Last reply 4 weeks ago
OCR A-level Computer Science Paper 1 (H446/01) - 12th June 2023 [Exam Chat]Last reply 2 months ago
OCR GCSE Computer Science Paper 1 (J277/01) - 19th May 2023 [Exam Chat]Last reply 3 months ago
AQA GCSE Computer Science Paper 2 (8525/2) - 25th May 2023 [Exam Chat]Last reply 5 months ago
AQA GCSE Computer Science Paper 1 (8525/1A, 1B, 1C) - 19th May 2023 [Exam Chat]Last reply 5 months ago
AQA Computer Science NEA and Computer science paper 1 question