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

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

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!