Dylan Bozarth
Dylan Bozarth

Reputation: 1694

setTimeout in React, not being able to clear

I am trying to have my code fire 3 seconds after the button being clicked. I can't manage to do this though. Either it is firing immediately or it fires and doesn't stop.

<Button onClick={setTimeout(() => window.location.reload(false), 4000), clearTimeout()}>Spin</Button>

It has been a long time since I've used setTimeout in any way, so it's probably a very simple fix.

Apologies if there is a similar question to this, I couldn't find anything that was relating to this enough to help me out.

Upvotes: 1

Views: 105

Answers (1)

Boussadjra Brahim
Boussadjra Brahim

Reputation: 1

You should add an event handler (method) like an arrow function :

onClick={()=>{setTimeout(() => window.location.reload(false), 4000); clearTimeout()}}

or onClick={someFunction}

function someFunction(){
  setTimeout(() => window.location.reload(false), 4000); clearTimeout()
} 

Upvotes: 1

Related Questions