Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

187
Views
trying to understand how setInterval/clearInterval work (in a solana web3 example)

I was going through the following tutorial to implement a simple web3 app:https://learn.figment.io/tutorials/chakra-ui-with-solana-dapps#realtime-account-updates-using-polling-and-custom-react-hooks

This sets up an interval to monitor the status of an account so that it can display up to date information.

The function where the interval is set up is as follows:

function useSolanaAccount() {
const [account, setAccount] = useState(null);
const { connection } = useConnection();
const wallet = useWallet();

const init = useCallback(async () => {

    if(wallet.publicKey)
        console.log("usecallback publicKey:", wallet.publicKey.toString());
    else
        console.log("usecallback publicKey: None");
        
    if (wallet.publicKey) {
        let acc = await connection.getAccountInfo(wallet.publicKey);
        setAccount(acc);
    }

}, [wallet, connection]);

useEffect(() => {
    if (wallet.publicKey) {
        setInterval(init, 1000);
        console.log("use effect publicKey:", wallet.publicKey.toString());
    }
    else{
        console.log("clear interval");
        clearInterval(init);
    }
}, [init, wallet]);

return { account };

}

I've been trying to understand how to cancel the interval, and i really don't get it. From the console log it clearly reaches the code "clearInterval(init)", but it doesn't actually seem to do anything. I've tried making init a global thing, which i read in another stackoverflow post as a possible solution, but that didn't help... Any help would be greatly appreciated!

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!