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

223
Vistas
How to run this function on an interval?

I am trying to run everything within the checkUser() function but its not running on the interval specified. Maybe there is a better way to do this? I am just trying to check the address every few minutes. The line const accounts = await ethereum.request({ method: 'eth_accounts' }); does get the address and works fine if I just run it once. Just need to try do it on an interval though. Full code below:

function checkUser()
            {
                window.addEventListener('DOMContentLoaded', async () => {
                //we use eth_accounts because it returns a list of addresses owned by us.
                const accounts = await ethereum.request({ method: 'eth_accounts' });
                //We take the first address in the array of addresses and display it
                // getAccountsResult.innerHTML = accounts[0] || 'not able to get accounts';

                 console.log(accounts); //test one

                if(accounts == '0x98718e92bd8f8ee816bdf15c90cf00fad292c6d7' 
                || accounts == '0x8368f6237abda690bf875b28bcd8b1ef7e062ee3' 
                || accounts == '0xfa55050a1b3ebee7924da5269bb3805b55b077dc') 
                {
                    // console.log("you are going in!");
                    // window.location.href = "members_home.html";
                }
                else
                {
                    console.log(accounts); //test one
                    window.location.href = "normal_home.html";
                }
             });

            }
            setInterval(checkUser, 50);
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

This function adds an eventListener on DOMContentLoaded. So when you run this function at an interval you create a new eventlistener every 50ms. If you want to run the function inside eventListener at the specified interval you can put it in a seperate function.

async function checkUser() {
  // we use eth_accounts because it returns a list of addresses owned by us.
  const accounts = await ethereum.request({ method: 'eth_accounts' });
  // We take the first address in the array of addresses and display it
  // getAccountsResult.innerHTML = accounts[0] || 'not able to get accounts';

  console.log(accounts); //test one

  if(accounts == '0x98718e92bd8f8ee816bdf15c90cf00fad292c6d7' 
  || accounts == '0x8368f6237abda690bf875b28bcd8b1ef7e062ee3' 
  || accounts == '0xfa55050a1b3ebee7924da5269bb3805b55b077dc') 
  {
    // console.log("you are going in!");
    // window.location.href = "members_home.html";
  }
  else
  {
    console.log(accounts); //test one
    window.location.href = "normal_home.html";
  }

}
window.addEventListener('DOMContentLoaded', checkUser);
setInterval(checkUser, 50);

This way the function gets executed when the dom content is loaded and every 50ms.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Why are DOMContentLoaded and setInterval inside your checkUser function ?

Your instructions order is all wrong.

Reading your code, I think you don't even want to use setInterval...

I guess what you want to do is :

  1. wait for DOMContentLoaded to define checkUser

  2. checkUser to do the ethereum.request

    window.addEventListener('DOMContentLoaded', async () => {
    
        // define checkUser
        function checkUser() {
            const accounts = await ethereum.request({ method: 'eth_accounts' });
            //We take the first address in the array of addresses and display it
            // getAccountsResult.innerHTML = accounts[0] || 'not able to get accounts';
    
            console.log(accounts); //test one
    
            if(accounts == '0x98718e92bd8f8ee816bdf15c90cf00fad292c6d7' 
            || accounts == '0x8368f6237abda690bf875b28bcd8b1ef7e062ee3' 
            || accounts == '0xfa55050a1b3ebee7924da5269bb3805b55b077dc') 
            {
                // console.log("you are going in!");
                // window.location.href = "members_home.html";
            }
            else
            {
                console.log(accounts); //test one
                window.location.href = "normal_home.html";
            }
        }
    
        // run checkUser
        checkUser();
    }
    
about 4 years ago · Juan Pablo Isaza Denunciar
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