How can I troubleshoot non-functional traffic lights in vue.js?

I've recently built a traffic light using vue.js, but unfortunately, it's not functioning as expected. The colors (red, yellow, and green) are supposed to change according to the time duration, but for some reason, this isn't happening. Can anyone spot the issue?

CSS

#screen {
width: 450px;
height: 740px;
background: #222; 
border-radius: 12px;
margin: auto;
padding: 23px;
}

.light {
    width: 230px;
    height: 270px;
    display: inline-block;
    opacity: 0.2;
    border-radius: 100%;
    transition: opacity 10s;
    margin-bottom: 12px;
}

.active {
    opacity: 1;
}
.red {
    background: red;
}

.yellow {
    background: yellow;
}

.green {
    background: green;
}

My HTML I have created the necessary divs.


<div id="screen">
<div id="light red"  :class="{active: now=='red'}"></div>
<div id="light yellow"  :class="{active: now=='yellow'}"></div>
<div id="light green"  :class="{active: now=='green'}"></div>
</div>

and here is my Vue.js code Everything appears to be in place and there are no errors in the console. Yet, the functionality still seems to be off. Any suggestions on what might be causing this issue?

class State {
  constructor(name, dur, next){
    this.name = name;
    this.dur = dur;
    this.next = next;
  }
}

class Controller {
  trigger(state, callback){
    callback(state);
    setTimeout(()=>{
      this.trigger(state.next, callback);
    }, state.dur * 10)
  }
}


var app = new Vue({
el: '#screen', 
  data:{
    now: 'red'
  },
  mounted(){
    var controller = new Controller();

    var red = new State('red', 2);
    var yellowRed = new State('yellow', 1);
    var yellowGreen = new State('yellow', 1);
    var green = new State('green', 3);

    red.next = yellowRed;
    yellowR.next = green;
    green.next = yellowGreen;
    yellowG.next = red;

    controller.trigger(red, (state)=>{
      this.now = state.name;
    });

  }
})

Could I be overlooking something? Should I consider rewriting my function?

Answer №1

There are a few issues that need to be addressed:

<div class="light red"  :class="{active: now=='red'}"></div>
<div class="light yellow"  :class="{active: now=='yellow'}"></div>
<div class="light green"  :class="{active: now=='green'}></div>

The attribute should be class instead of id.

var red = new State('red', 2);
var yellowRed = new State('yellow', 1);
var yellowGreen = new State('yellow', 1);
var green = new State('green', 3);

red.next = yellowRed;
yellowRed.next = green;
green.next = yellowGreen;
yellowGreen.next = red;

You named yellowRed and yellowGreen, but then mistakenly used yellowR and YellowG.

I have created an updated fiddle here for you to review https://jsfiddle.net/Lo50j4rw/. I also made some adjustments to the duration.
Also, in my country, the light turns green after red :D

Answer №2

One issue you faced was using id instead of class for your HTML light elements. Additionally, the timing choices were odd - each light had a short duration but the transition lasted 10 seconds.

To correct these errors, consider the following:

class State {
  constructor(name, dur, next) {
    this.name = name;
    this.dur = dur;
    this.next = next;
  }
}

class Constroller {
  trigger(state, callback) {
    callback(state);
    setTimeout(() => {
      this.trigger(state.next, callback);
    }, state.dur * 1000)
  }
}

new Vue({
  el: '#screen',
  data: {
    now: 'red'
  },
  mounted() {
    var constroller = new Constroller();

    var red = new State('red', 2);
    var yellowRed = new State('yellow', 1);
    var yellowGreen = new State('yellow', 1);
    var green = new State('green', 3);

    red.next = yellowRed;
    yellowRed.next = green;
    green.next = yellowGreen;
    yellowGreen.next = red;

    constroller.trigger(red, (state) => {
      this.now = state.name;
    });

  }
})
#screen {
width: 450px;
height: 740px;
background: #222; 
border-radius: 12px;
margin: auto;
padding: 23px;
}

.light {
    width: 230px;
    height: 270px;
    display: inline-block;
    opacity: 0.2;
    border-radius: 100%;
    transition: opacity 0.3s;
    margin-bottom: 12px;
    background-color: white;
}

.active {
    opacity: 1;
}
.red {
    background: red;
}

.yellow {
    background: yellow;
}

.green {
    background: green;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="screen">
  <div class="light red" :class="{active: now=='red'}"></div>
  <div class="light yellow" :class="{active: now=='yellow'}"></div>
  <div class="light green" :class="{active: now=='green'}"></div>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Hovering over the Second NavBar will display a block instead of just a line

Hey there, I'm facing an issue on my website with the second navbar where I display categories. Out of the 9 categories listed, 2 only have one line of text each. This causes the hover effect to only highlight the text line itself and not the entire b ...

Resetting the state of toggle/click states in AJAX and jQuery

Currently, I am encountering a small dilemma with a .on function and AJAX in conjunction with a mobile menu. The mobile menu is located in the header of a site that relies heavily on AJAX for its content loading. This poses an issue because when an AJAX ca ...

Struggling to run two separate single page applications concurrently on a single server with nginx

UPDATE: I have made revisions to this post in order to provide a clearer explanation of the issue. The initial post has been replaced with this updated version. We are dealing with two single-page applications that must be accessible from the same domain ...

Running a repeated script

I recently discovered a fantastic script called Typer that creates a typewriter effect. However, I noticed that it does not have a built-in method to loop the animation once it reaches the end. Does anyone have any suggestions on how to achieve this? t ...

Display issue with Google Chart

Could someone please help me identify why these charts are not showing up? This code was functioning properly in a previous project. I copied the same code to a new project, only adding the master page. However, now the charts are not appearing. All I can ...

Convert HTML tables from Yahoo Pipes into an array of JSON objects

Currently, I am experimenting with Yahoo Pipes in an attempt to convert multiple tables within a DIV into an array of JSON objects. At the moment, the HTML is being successfully parsed. Here is my current setup on Yahoo Pipes I envision each table cell ...

Guide to positioning an item at the bottom of the screen using Material UI

Can anyone help me align a button to the bottom of the screen so that it remains in place even when scrolling through a list? I've tried adjusting the code but can't seem to get it right. This is how my current screen appears, with the button al ...

Adding a new .row in Angular based on an ngFor condition

As a newcomer to Angular, I decided to take on their tutorial with a few modifications. I have created an array as follows: export const HEROES: Hero[] = [ { id: 11, name: 'Mr. Nice' }, { id: 12, name: 'Narco' }, { id: 13, name: ...

DataTables.js, the powerful JavaScript library for creating interactive tables, and its compatible counterpart

I'm currently making some changes to a dynamic table that require enabling a vertical scroll bar. As a result, I need to implement this vertical scroll bar mechanism on an existing table. The code in our project utilizes dataTables.js version 1.5.2 ...

What is the method to modify a section of a URL based on form input?

I am currently developing a website that allows users to interact with product images by clicking buttons, entering text into forms, and using a slider to view different angles of the product. My main goal is to extract user input from form fields and dyn ...

What is the method to obtain the current data source filter for a Kendo UI grid in a Vue application?

Looking to retrieve the current datasource filter using Kendo UI for jQuery? Simply use dataSource.filter() However, when it comes to the Vue.js version, finding the equivalent seems challenging. I've prepared a demo here. The current filter is appli ...

What is the best way to create an element with adjustable width and height dimensions?

Looking for advice on creating a wrapper element without having to repeatedly adjust the height and width properties. The current code snippet sets a fixed size for the wrapper, but is there a more efficient way to achieve this? .search{ background- ...

The distinction between storing data and component data becomes apparent when using Vuex in conjunction with a persisted state

Below is my post.js file in the store directory: import axios from 'axios' import createPersistedState from "vuex-persistedstate" export default { namespaced: true, state: { sample_data: 'Welcome!!', l ...

What is the best way to ensure the website title is displayed properly on a mobile browser?

Concern I have been encountering difficulties while constructing a portfolio website using bootstrap 5. The issue arises with the navbar, causing the Title to overlap the hamburger menu and extend beyond the edge of the screen. Strangely enough, this prob ...

Determining if a Website is Responsive with PHP

One way to determine if a website is responsive or not is by analyzing its HTML code. A previous inquiry on this topic can be found here: . This method only focuses on checking for the <meta name="viewport" content="width=device-width"> tag. It&apos ...

No animated scrolling occurring

I have been struggling with this problem for a while now and despite having gone through 100 questions, I still can't find a solution as to why my code snippet is failing. Here is the jQuery code I am using: $("#readmore").click(function(event){ ...

AJAX Showdown: Comparing jQuery's AJAX API to JavaScript's XHR

tl;dr: I have two different scripts that appear to be identical, but one works while the other does not. Why is this? Let me provide some context for this post. I am working on creating an image uploading form that utilizes AJAX requests to generate a dyn ...

Error: html2canvas encountered an uncaught undefined promise

Currently, I am working with an HTML canvas tag: <canvas id="myCanvas"></canvas> I have successfully created a drawing on this canvas, and it looks exactly how I wanted it to. However, when trying to convert it to a PNG file using html2canvas ...

Is it possible to detect when a user reaches the end of a div without using a scroll event?

Seeking a JavaScript solution that can identify when a user reaches the bottom of a div with overflow: auto. While there are numerous solutions on Stack Overflow utilizing the onscroll event, I am curious if this can be accomplished using newer technology ...

What are some tips for using CSS display grid to effectively showcase my HTML content?

Visit my About Me page with custom menu styling Any tips on aligning the body with the grid function to achieve this particular design? ...