DEV Community

Steve
Steve

Posted on

1

Help! (JS scripting question)

I've been coding for a grand total of three weeks, and have learned just enough HTML5, CSS, and JS to be dangerous.

I've created the typical click-the-smiley-face game for my JS class, and I'd like to add a feature not covered in the curriculum. Hoping someone can point me in the right direction without actually giving me the answer - I'd like to figure it out on my own as much as possible.

I'm calling the function startGame(), and when the user clicks anywhere but the designated target, it calls the gameOver() function.

I'd like to set a timer for ten seconds that, if no selection is made, the script will automatically call the "onclick=function gameOver()" command.

I've played with various iterations and placements of setTimeout() and clearTimeout(), but can't seem to get it to work.

1) Am I on the right track? Any tips on scripting this?
2) Is it possible to display a small countdown clock in the upper right hand corner?

Thanks in advance for any help or tips you can throw my way!

(Not sure of the rules regarding this, but I can post a link to my codepen if that's allowed.)

$150K MiniMax AI Agent Challenge — Build Smarter, Remix Bolder, Win Bigger!

Join the $150k MiniMax AI Agent Challenge — Build your first AI Agent 🤖

Developers, innovators, and AI tinkerers, build your AI Agent and win $150,000 in cash. 💰

Read more →

Top comments (2)

Collapse
 
khuongduybui profile image
Duy K. Bui
  1. Call setTimeout() at the entry point of your application. If you are not using any framework, that can just be in a <script> tag at the end of your <body> :)
  2. setTimeout() only runs once, so to do a countdown, you need to call setTimeout again within the block called by setTimeout. Or you can just use setInterval().
Collapse
 
sehardwick profile image
Steve

Thanks - I'll play with this in the morning!

Neon image

Next.js applications: Set up a Neon project in seconds

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Get started →

👋 Kindness is contagious

Explore this practical breakdown on DEV’s open platform, where developers from every background come together to push boundaries. No matter your experience, your viewpoint enriches the conversation.

Dropping a simple “thank you” or question in the comments goes a long way in supporting authors—your feedback helps ideas evolve.

At DEV, shared discovery drives progress and builds lasting bonds. If this post resonated, a quick nod of appreciation can make all the difference.

Okay