在NodeJS中的循环中,每隔X秒调用一个函数



我的目标:每X秒调用一个函数。

我正在使用Coinbase pro API获取我的钱包信息。每秒有API调用的限制。所以我想每5秒为每个钱包调用一次函数getWalletHistory((。

我所拥有的:我的函数getWalletList((被调用,然后我等待5秒钟来调用我的函数getWalletHistory((。

我想要的是:每隔5秒为每个钱包调用我的函数getWalletHistory((。

var wallet_list = [];
getWalletList();
function getWalletList(){
authedClient.getAccounts().then(wallet_list_response => {
wallet_list_response.forEach(async wallet => {
console.log("WALLET LIST:" + wallet.id);
wallet_list.push(wallet);
});
console.log('**********END LOOP WALLET LIST');
getWalletHistory(wallet_list);
}).catch(error => {
// handle the error
console.log('ERROR getAccounts');
//console.log(error);
});
}
const getWalletHistory = async function(wallet_args){
wallet_args.forEach(async wallet => {
setTimeout(function(){
authedClient.getAccountHistory(wallet.id).then(order_list => {
console.log('*********response');
}).catch(error => {
// handle the error
console.log('ERROR getAccountHistory');
//console.log(error);
});
}, 5000);
});
}

UPDATE根据Erenn的评论,我使用SetInterval而不是SetTimeout:

var wallet_list = [];
getWalletList();
function getWalletList(){
authedClient.getAccounts().then(wallet_list_response => {
wallet_list_response.forEach(async wallet => {
console.log("WALLET LIST:" + wallet.id);
setInterval(() => getWalletHistory(wallet), 5000);
});
console.log('**********END LOOP WALLET LIST');
}).catch(error => {
// handle the error
console.log('ERROR getAccounts');
//console.log(error);
});
}
const getWalletHistory = async function(wallet){
console.log('*********getWalletHistory:' + wallet.currency);
authedClient.getAccountHistory(wallet.id).then(order_list => {
console.log('*********response order_list');
}).catch(error => {
// handle the error
console.log('ERROR getAccountHistory');
//console.log(error);
});
}

我现在拥有的是:对于无限循环中的所有钱包,每5秒调用一次getAccountHistory((。我想每5秒调用一次getAccountHistory((,每次只调用1个钱包。

您可以维护一个列表队列,setInterval可以每5000毫秒对其进行操作。下面是更新的片段查看getWalletHistories及其注释

var wallet_list = [];
getWalletList();
// responisble for maintaining queue (in the form of array) and setting interval
// Create a new reference of list, pop the item, calls getWalletHistory
// and once promise gets resolved, move the item back in the list (front of the array)
function getWalletHistories(walletList) {
let interval;
// clear interval counter if getWalletHistories called once again
if (interval) {
clearInterval(interval);
}
const _list = [...walletList];
interval = setInterval(() => {
// pop wallet item from list for get wallet item history
const wallet = _list.pop();
getWalletHistory(wallet).then(() => {
// push the popped wallet item back to the front of list
_list.unshift(wallet);
});
}, 5000);
}
function getWalletList() {
authedClient
.getAccounts()
.then((wallet_list_response) => {
getWalletHistories(wallet_list_response);
console.log("**********END LOOP WALLET LIST");
})
.catch((error) => {
// handle the error
console.log("ERROR getAccounts");
//console.log(error);
});
}
function getWalletHistories(walletList) {
let interval;
// clear interval counter if getWalletHistories called once again
if (interval) {
clearInterval(interval);
}
const _list = [...walletList];
interval = setInterval(() => {
// pop wallet item from list for getting wallet item history
const wallet = _list.pop();
getWalletHistory(wallet).then(() => {
// push the popped wallet item back to the front of list
_list.unshift(wallet);
});
}, 5000);
}
const getWalletHistory = async function (wallet) {
console.log("*********getWalletHistory:" + wallet.currency);
// return the wallet history promise
return authedClient
.getAccountHistory(wallet.id)
.then((order_list) => {
console.log("*********response order_list", order_list);
})
.catch((error) => {
// handle the error
console.log("ERROR getAccountHistory");
//console.log(error);
});
};

最新更新