sei-ec-remote / project-1-issues

Open new issues here
1 stars 2 forks source link

setTimeout #137

Closed melsil11 closed 2 years ago

melsil11 commented 2 years ago

What's the problem you're trying to solve?

I want the computer to wait a few seconds before taking its turn

Post any code you think might be relevant (one fenced block per file)


let playerTurn = true
const matchCheck = (event) => {
    console.log('in matchCheck, event.target.id',event.target.id)
    let selection = event.target.id
    console.log('matchCheck, choices', choices)
    if (selection === 'faceCard1') {
        result = document.getElementById("results").innerText='Congratulations, You Win! Press the button to play again.';
        removeHandler()
    //    need to stop game
    } else  {
        console.log('got here')
        playerTurn = false
        console.log(playerTurn)
        result = document.getElementById("results").innerText='Sorry, no match. It is the computers turn.';

    } 
    console.log('in matchCheck, result',result)
     // remove the card choice from the array for computers turn
    choices.splice(choices.indexOf(selection),1)
    console.log(choices) 
    if (playerTurn === false) {
        setTimeout(computerChooses(),100000)
        clearTimeout()
        computerChoice()

    }

}

function removeHandler() {
    document.getElementById('faceCard1').removeEventListener('click', matchCheck);
    document.getElementById('faceCard2').removeEventListener('click', matchCheck);
    document.getElementById('faceCard3').removeEventListener('click', matchCheck);
}

const myTimeout = setTimeout(computerChooses, 10000);
function stopWait() {
    clearTimeout(myTimeout);
}```
stopWait()
### If you see an error message, post it here. If you don't, what unexpected behavior are you seeing?

no error messages
### What is your best guess as to the source of the problem?

The syntax is wrong or I am putting it in the wrong function 
### What things have you already tried to solve the problem?

I created its own function to call it, I put on the functions when they are executed

### Paste a link to your repository here[
](https://github.com/melsil11/Find-the-Puppy)
scarletpuma commented 2 years ago

Make sure you look at the setTimeout() syntax on mdn

melsil11 commented 2 years ago

I got it to wait but the other functions would not run after

scarletpuma commented 2 years ago

You don't actually have to clearTimeout there. Since it's a timer it ends on its own

melsil11 commented 2 years ago

I had it set on the wrong function it is working great now