首页 > 解决方案 > 循环不适用于间隔

问题描述

我正在使用 Firebase 和 Twitter API 创建一个 Twitter 机器人来取消关注非活动帐户。

这是 Nodejs 代码:

// Get the screen_names in `to_unfollow` table
firebase.database().ref("to_unfollow/" + settings.PERSON_TWITTER_HANDLE).on("value", function(snapshot) {

// Functional Loop
var i = 0;
function timedLoop() { // unFollows the user after every `x` seconds

      /*=============================================>>>>>
      = Thing to be done =
      ===============================================>>>>>*/

        function snapshotToArray(snapshot) { // This function converts the Snapshot data into an array
            var returnArr = [];

            snapshot.forEach(function(childSnapshot) {
                var item = childSnapshot.val();
                item.key = childSnapshot.key;

                returnArr.push(item);
            });

            return returnArr;
        };

        var screen_name_to_unfollow = snapshotToArray(snapshot)[i].key;

        console.log(screen_name_to_unfollow);

      /*= End of Thing to be done =*/
      /*=============================================<<<<<*/

      // Increase value of variable `i` by 1. (Increment)
      i++;

      // How many times to loop
      if(i < 5000) {
          setTimeout( timedLoop, 1000*20 ); // timedLoop();
      }

  }

  timedLoop(); // Run the loop

});

在这里,我的循环运行良好。该screen_name_to_unfollow变量每 20 秒后登录到控制台。


但是当我添加执行代码以取消关注人员时,循环工作......但不是有时间间隔。它只是不断地取消关注人。

以下是我的代码的样子:

// Get the screen_names in `to_unfollow` table
firebase.database().ref("to_unfollow/" + settings.PERSON_TWITTER_HANDLE).on("value", function(snapshot) {

// Functional Loop
var i = 0;
function timedLoop() { // unFollows the user after every `x` seconds

      /*=============================================>>>>>
      = Thing to be done =
      ===============================================>>>>>*/

        function snapshotToArray(snapshot) { // This function converts the Snapshot data into an array
            var returnArr = [];

            snapshot.forEach(function(childSnapshot) {
                var item = childSnapshot.val();
                item.key = childSnapshot.key;

                returnArr.push(item);
            });

            return returnArr;
        };

        var screen_name_to_unfollow = snapshotToArray(snapshot)[i].key;

        console.log(screen_name_to_unfollow);

        // UnFollow
        T.post('friendships/destroy', { screen_name: screen_name_to_unfollow },  function (err, data, response) {
          console.log('T.Post', new Date());
          if(!err){
            console.log(settings.PERSON_NICKNAME + " follower " + screen_name_to_unfollow + " unfollowed.");

            // Create an `unfollowed` table and insert the screen_name there
            firebase.database().ref("unfollowed").child(settings.PERSON_TWITTER_HANDLE).update({
              [screen_name_to_unfollow]: {
                connection: "unfollowed"
              }
            });

            // Delete the screen_name from `to_unfollow` table
            firebase.database().ref("to_unfollow/" + settings.PERSON_TWITTER_HANDLE).child(screen_name_to_unfollow).remove();

          } else{
            console.log(err);
          }
        });

      /*= End of Thing to be done =*/
      /*=============================================<<<<<*/

      // Increase value of variable `i` by 1. (Increment)
      i++;

      // How many times to loop
      if(i < 5000) {
          setTimeout( timedLoop, 1000*20 ); // timedLoop();
      }

  }

  timedLoop(); // Run the loop

});

在终端中,您可以看到间隔没有解决。

T.Post 2018-05-31T02:11:27.234Z
Wesbos follower AnnSaid unfollowed.
T.Post 2018-05-31T02:11:27.914Z
Wesbos follower AnnyShivang unfollowed.
T.Post 2018-05-31T02:11:28.865Z
Wesbos follower AntJanus unfollowed.
T.Post 2018-05-31T02:11:29.888Z
Wesbos follower AnthonyCatonPR unfollowed.
T.Post 2018-05-31T02:11:30.975Z
Wesbos follower AppleLaa unfollowed.
T.Post 2018-05-31T02:11:31.733Z
Wesbos follower AsyrafDuyshart unfollowed.

在这种情况下,我该怎么做才能使循环像以前一样工作?我希望每 20 秒后取消关注少数不活动的 Twitter 帐户,因为我不想陷入速率限制。

提前致谢 :-)

标签: javascriptnode.jsloopsfirebasetwitter

解决方案


问题是.onfirebase 的侦听器,每次T.post结束时都会被调用,因为您要从to_unfollow/.

因此,每次T.post结束时,您都会创建一个额外的setTimeout循环,如果您让该代码运行几分钟,您将对 twitter 执行数千个请求。

改为使用.once将解决您的问题。并且在取消关注快照中的所有用户之后,您应该请求另一个设置为 firebase。


推荐阅读