Day 11 – Junk food selector

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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s