'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);
Solution 1:[1]
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 :
wait for
DOMContentLoadedto definecheckUsercheckUserto do theethereum.requestwindow.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(); }
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | E-telier |
