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.)

ACI image

ACI.dev: The Only MCP Server Your AI Agents Need

ACI.dev’s open-source tool-use platform and Unified MCP Server turns 600+ functions into two simple MCP tools on one server—search and execute. Comes with multi-tenant auth and natural-language permission scopes. 100% open-source under Apache 2.0.

Star our GitHub!

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!

Image of Stellar post

How a Hackathon Win Led to My Startup Getting Funded

In this episode, you'll see:

  • The hackathon wins that sparked the journey.
  • The moment José and Joseph decided to go all-in.
  • Building a working prototype on Stellar.
  • Using the PassKeys feature of Soroban.
  • Getting funded via the Stellar Community Fund.

Watch the video

👋 Kindness is contagious

Dive into this insightful write-up, celebrated within the collaborative DEV Community. Developers at any stage are invited to contribute and elevate our shared skills.

A simple "thank you" can boost someone’s spirits—leave your kudos in the comments!

On DEV, exchanging ideas fuels progress and deepens our connections. If this post helped you, a brief note of thanks goes a long way.

Okay