Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

186
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda