如何运行这个函数在一个区间上?
How to run this function on an interval?
我正在尝试 运行 checkUser()
函数中的所有内容,但不是 运行 在指定的时间间隔内。也许有更好的方法来做到这一点?我只是想每隔几分钟检查一次地址。 const accounts = await ethereum.request({ method: 'eth_accounts' });
行确实获取了地址并且如果我只 运行 它一次就可以正常工作。不过,只需要尝试间隔一段时间即可。完整代码如下:
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);
此函数在 DOMContentLoaded 上添加一个 eventListener。因此,当您 运行 每隔一段时间使用此函数时,您每 50 毫秒创建一个新的事件监听器。如果您想 运行 在指定的时间间隔内使用 eventListener 中的函数,您可以将它放在一个单独的函数中。
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);
这样,当加载 dom 内容时,每 50 毫秒执行一次该函数。
为什么 DOMContentLoaded
和 setInterval
在你的 checkUser
函数中?
你的指令顺序全错了。
阅读您的代码,我认为您甚至不想使用 setInterval...
我猜你想做的是:
等待DOMContentLoaded
定义checkUser
checkUser
做 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();
}
我正在尝试 运行 checkUser()
函数中的所有内容,但不是 运行 在指定的时间间隔内。也许有更好的方法来做到这一点?我只是想每隔几分钟检查一次地址。 const accounts = await ethereum.request({ method: 'eth_accounts' });
行确实获取了地址并且如果我只 运行 它一次就可以正常工作。不过,只需要尝试间隔一段时间即可。完整代码如下:
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);
此函数在 DOMContentLoaded 上添加一个 eventListener。因此,当您 运行 每隔一段时间使用此函数时,您每 50 毫秒创建一个新的事件监听器。如果您想 运行 在指定的时间间隔内使用 eventListener 中的函数,您可以将它放在一个单独的函数中。
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);
这样,当加载 dom 内容时,每 50 毫秒执行一次该函数。
为什么 DOMContentLoaded
和 setInterval
在你的 checkUser
函数中?
你的指令顺序全错了。
阅读您的代码,我认为您甚至不想使用 setInterval...
我猜你想做的是:
等待
DOMContentLoaded
定义checkUser
checkUser
做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(); }