Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

197
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda