Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 1380
Alex Hales
  • 0
Alex HalesTeacher
Asked: May 30, 20222022-05-30T14:16:35+00:00 2022-05-30T14:16:35+00:00

javascript – Some code not executing when program is reset

  • 0

[ad_1]

I am working on a match color game. After the guesses are completed the initialize function is called, which resets the program. After the first set of guesses, when the initialize function is called again, code below the initialize function (two setTimeouts), that asks the user where a color is does not fire again. Thanks for the help.

html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>HTML 5 Boilerplate</title>
    <script src="https://code.jquery.com/jquery-3.6.0.js" integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk=" crossorigin="anonymous"></script>
    <link rel="stylesheet" href="styles.css">
  </head>
  <body style="pointer-events: none">
      <div class="question">
          <div class="questionbox">
              <p>Where is:</p>
          </div>
      </div>
    <div class="container">
        <div class="container-box">
            <div class="colorbox"></div>
            <div class="colorbox"></div>
            <div class="colorbox"></div> 
            <div class="colorbox"></div>
            <div class="colorbox"></div>
            <div class="colorbox"></div>
        </div>
    </div>
    <div class="score-container">
        <div class="score">Score: <span id="score">0</span></div>
    </div>
    <div class="button">
    <button type="button" id="btn">Restart</button>

    </div>
    <script src="script.js"></script>
  </body>
</html>

css

body {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    height: 100vh;
}

.container {
    height: 100vh;
    width: 100%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
}

.question {
    position: absolute;
    height: 100vh;
    width: 100%;
    display: none;
    flex-direction: column;
    justify-content: center;
    align-items: center;
}
.questionbox {
    display: flex;
    align-items: center;
    justify-content: center;
    border: 1px solid black;
    background-color: red;
    height: 400px;
    width: 600px;
    color: white;
    font-size: 33px;
}


.container-box {
    width: 600px;
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    height: 400px;
}

.colorbox {
    display: flex;
    height: 200px;
    width: 200px;
    border: 1px solid black;
    box-sizing: border-box;
    
}

.hide {
    background-color: grey !important;
}

.open {
   
    border: 40px white;
    cursor: default;
  }
  .button {
      width: 100vw;
      display: flex;
      flex-direction: row;
      justify-content: center;
  }
  #btn {
      display: flex;
      position: absolute;
      bottom: 50px;
      justify-content: center;
      
      text-align: center;

  }
  .score-container {
      display: flex;
      position: absolute;
      width: 100vw;
      justify-content: center;
      bottom: 100px;
  }

  .border {
      border: 8px solid #66ff00 !important;
  }
  .border-red {
    border: 8px solid #EE4B2B !important;
}

javascript

let colors = ["red", "yellow", "blue", "green", "orange", "purple"],
    $fullbox = $('.container'),
    $popup = $('.question'),
    gamecolor = [],
    $restart = $('#btn'),
    clicks = 0,
    guessColor = [],
    score = 0,
    guess;

window.onload = initialize();
// initialize board
function initialize () {
    clicks = 0;
    $('.colorbox').removeClass('border');
    $('.colorbox').removeClass('border-red');
    $('.colorbox').each(function () {
    $('body').css("pointer-events", "none");
    $('.colorbox').removeClass('hide');
     $(this).css("border", "2px solid black");
     let randColor = colors[Math.floor(Math.random() * colors.length)];
     $(this).css("background-color", `${randColor}`);
     setTimeout(hide, 3000);  
    })
}


function hide() {
    $('.colorbox').each(function () {
    $(this).addClass('hide');
    $('body').css("pointer-events", "auto");
    $('.colorbox').css("pointer-events", "auto");
    })
}
//restart
$('#btn').on('click', () => {
    initialize();
})

setTimeout(() => {
    $popup.css('display', 'flex');
    guessColor = colors[Math.floor(Math.random() * colors.length)];
    $('.questionbox').find('p').after(`<span>&nbsp; ${guessColor}?</span>`);
}, 2500)

setTimeout(() => {
    $popup.css('display', 'none');
    $('body').css("pointer-events", "auto");
}, 6000)



$('.container').on('click', '.colorbox', function () {
    console.log($(this));
    $(this).css("pointer-events", "none");
    clicks++;
    guess = $(this)[0].style.backgroundColor;
    console.log(guess);
    $(this).removeClass('hide');
    console.log(guessColor);
    console.log(guess);
    if (guess === guessColor) {
        score;
        score++;
        $('#score').html(score);
        $(this).addClass('border');
    } else {
        $(this).addClass('border-red');
        // $(this).css("border", "4px solid red");
    }
    if (clicks >= colors.length) {
        setTimeout(initialize, 800);
        $('body').css("pointer-events", "none");
    }
    console.log($(this));  
     
});

[ad_2]

  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 4777 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 1063 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 1009 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.