Estoy tratando de ejecutar todo dentro de la función checkUser() pero no se ejecuta en el intervalo especificado. Tal vez hay una mejor manera de hacer esto? Sólo intento comprobar la dirección cada pocos minutos. La línea const accounts = await ethereum.request({ method: 'eth_accounts' }); obtiene la dirección y funciona bien si solo lo ejecuto una vez. Sin embargo, solo necesito intentar hacerlo en un intervalo. Código completo a continuación:
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);Esta función agrega un eventListener en DOMContentLoaded. Entonces, cuando ejecuta esta función en un intervalo, crea un nuevo detector de eventos cada 50 ms. Si desea ejecutar la función dentro de eventListener en el intervalo especificado, puede colocarla en una función separada.
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);De esta forma, la función se ejecuta cuando se carga el contenido del dom y cada 50 ms.
¿Por qué están DOMContentLoaded y setInterval dentro de su función checkUser ?
Su orden de instrucciones es todo incorrecto.
Leyendo tu código, creo que ni siquiera quieres usar setInterval...
Supongo que lo que quieres hacer es:
espere a que DOMContentLoaded defina checkUser
checkUser para hacer el 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(); }