Welcome to the most politically UN-correct programming project ever! Okay, maybe not ever, but it’s pretty close. I was thinking about doing a rock-paper-scissors project, but wanted to mix it up a bit 🙂
Don’t know what you’re having with your hangover? Try the Junk food selector now!
So I learned:
- To make sure that the button cannot be pressed several times and thereby messing up the timeouts, I started the script with a variable called “lock” and set it to false, ie. “open”. The BF introduced me to this concept called a Software Design Pattern. The functionality is that the lock is open for the first time. Then right after the button is clicked the lock is set to true (meaning locked). Then the lock is opened again after the second setTimeout has run.
- I made an array of objects with the image url and the name of the food. To get a random one I again used the Math.random function.
- I used two setTimeout functions. The first one is inside the changeToRandom function. Before the changeToRandom function I made a variable called “stop” and set that to false. Inside the changeToRandom function I used the setTimeout function to run the changeToRandom every 100 miliseconds (that’s what makes it blink) and then after I called that function (changeToRandom) I made a new setTimeout function that set the variable “stop” to true after 5000 miliseconds, and that is what makes it stop blinking.