DEV Community

Steve
Steve

Posted on

Set an inaction timeout in JavaScript

I posted a question here yesterday regarding being able to set an "inaction" timeout that would reset a JS game:

https://dev.to/sehardwick/help-js-scripting-question-2cda

Happy to say that with a little guidance, Stack Overflow research, and testing, I was able to come up with a solution!

After declaring <script> variables, I created a setTimeout method that called a function that was then defined with an alert and the necessary code to reset the game, and I ended up with:

setTimeout(functionName(), 15000) //to set a 15 second inactive period
  function functionName() 
    {
    alert("text message along with counter displaying how many correct guesses the player had")

//Remainder of code to reset the game 

    }

Super-psyched I finally got this to work!

Top comments (3)

Collapse
 
katnel20 profile image
Katie Nelson

You can also shorten it to a single line of code:

setTimeout(function(){ alert("message goes here"); }, 15000);
Collapse
 
sehardwick profile image
Steve

As it turns out, I've only accomplished half the task - I've basically created a game that lasts fifteen seconds. I need to address resetting the timer with each mouse click...

Collapse
 
katnel20 profile image
Katie Nelson • Edited

Then you will need to call the ClearTimeout function and pass it the var that the setTimeout function returned.