javascript - 循环不适用于间隔
问题描述
我正在使用 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 帐户,因为我不想陷入速率限制。
提前致谢 :-)
解决方案
问题是.on
firebase 的侦听器,每次T.post
结束时都会被调用,因为您要从to_unfollow/
.
因此,每次T.post
结束时,您都会创建一个额外的setTimeout
循环,如果您让该代码运行几分钟,您将对 twitter 执行数千个请求。
改为使用.once
将解决您的问题。并且在取消关注快照中的所有用户之后,您应该请求另一个设置为 firebase。
推荐阅读
- javascript - 如何在输入类型号(角度,js)中显示尾随零
- compiler-errors - 动态(可分配)向量的 gfortran 编译器错误
- google-apps-script - 如何在 googlesheet 中使用 xmlimport 函数从具有相同类名的 DIV 中获取不同的值?
- mysql - 使用 'IF' 检测 mysql 更新查询中的 NULL
- python - 如何将 array([]) 对象转换为 array([[]]) 对象?
- google-chrome - 在 chrome 中启用不安全的 websocket
- arduino - 如何检查从 eeprom 读取的 ssid/pass 是否良好
- javascript - jQuery 滚动功能正在滚动超过带有元素 id 的 div 的顶部
- c# - 我有错误 CS0136,但我找不到解决方案
- graphql - 如何分组和计数dgraph