【问题标题】:Call a function every X second in a loop in NodeJS在 NodeJS 的循环中每隔 X 秒调用一个函数
【发布时间】:2021-12-30 21:05:30
【问题描述】:

我的目标:每 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);
    });
}

更新 根据 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);
    });
}

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

【问题讨论】:

  • 我认为你想要 setInterval 而不是 setTimeout。
  • 谢谢,我更新了我的帖子。使用 setInterval 时出现新错误
  • 因为你是直接执行参数中的函数,所以试试() => getWalletHistory(wallet)
  • 没错,我没注意到!现在的问题是函数 getWalletHistory() 被无限次调用。每隔 5 秒,我会为所有钱包调用 getWalletHistory(),我希望它只为 1 个钱包调用我的函数。

标签: javascript node.js api settimeout coinbase-api


【解决方案1】:

您可以维护一个列表队列,其中 setInterval 可以每 5000 毫秒运行一次。下面更新了 sn-p 查看 getWalletHistories 及其 cmets

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);
    });
};

【讨论】:

  • 谢谢,它运行良好!我只是修改了 clearInterval 的条件 if (interval) { clearInterval(interval); } by: if(wallet.length === 0){ clearInterval(interval); }
猜你喜欢
  • 1970-01-01
  • 2018-03-15
  • 2015-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-24
  • 1970-01-01
相关资源
最近更新 更多