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

218
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
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