Ray Orole
Ray Orole

Reputation: 39

How to make a function that runs every x seconds within React/Nextjs

I want to make a nextjs page where a user can view the payment status of a specific transaction on the Ethereum blockchain. How do I loop this function every few seconds to check if the transaction status changed and change the UI?

Upvotes: 1

Views: 3377

Answers (1)

nolan
nolan

Reputation: 69

Have you considered using setTimeout?

const timer = setTimeout(() => {
    someFunction()
 }, 1000);

The second parameter is the timeout time between calls in ms. When your component unmounts make sure to clear the timeout by calling clearTimeout in your unmount or function returned by useEffect

clearTimeout(timer)

More info can be found in the docs here: https://developer.mozilla.org/en-US/docs/Web/API/setTimeout

Upvotes: 2

Related Questions