Traffic light using javascript

I want create traffic light controller.

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <link rel="stylesheet" href="style.css">
<body onload="timer;">

<div id="isiqfor">
    <div class="green"></div>
    <div class="yellow"></div>
    <div class="red"></div>

    <script src="script.js"></script>

CSS code:

    border: 10px solid black;
    padding: 10px 3px;
    width: 50px;
    height: 50px;
    border-radius: 50%;
    opacity: .3;
    background-color: green;
    background-color: yellow;
    background-color: red;

And JS file:

function myFun () {
    // body... 
    var green=document.getElementsByClassName("green")[0];
    var red=document.getElementsByClassName("red")[0];
    var yellow=document.getElementsByClassName("yellow")[0];;
    setTimeout(function () {
        /* body... */;;;

    setTimeout(function () {
        /* body... */;;;

    setTimeout(function () {
        /* body... */;;;


var timer = setInterval(function () {
    /* body... */

But problem is when page loads it must wait 13 second for beginning traffic light.How can solve this problem? I want when page loads green light has switched.

11 thoughts on “Traffic light using javascript”

  1. I simply couldn’t leave your site before suggesting
    that I really enjoyed the standard information an individual supply in your guests?

    Is going to be back often in order to check up on new posts

  2. Thanks for a marvelous posting! I truly enjoyed reading
    it, you will be a great author.I will be sure to bookmark your blog and definitely
    will come back sometime soon. I want to encourage you continue your great work, have
    a nice afternoon!

  3. 695819 675900I dont agree with this specific post. Even so, I did researched in Google and Ive located out that you are correct and I had been thinking inside the incorrect way. Continue producing quality material similar to this. 745856


Leave a Comment