我无法运行依赖于函数变量的循环

时间:2016-11-07 04:55:08

标签: javascript json node.js callback

好的,所以我花了最后一天试图解决问题,我对编码比较新,所以如果一团糟我很抱歉。我目前正在研究一个请求JSON的机器人,这是我到目前为止的代码

 const request = require('request');

 const bodyParser = require('body-parser');


global.count = 10;

for (var i = 1; global.count === 10; i++) {

var options = {
  url: 'https://www.the100.io/api/v1/groups/2127/users?page=' + i, //Returns 10 entries per page, so loop is to navigate pages
  headers: {
  'Authorization': 'Token token="Hidden for Privacy"'
  }
}

function callback(error, response, body) {
  if (!error && response.statusCode == 200) {
    var info = JSON.parse(body); //Also need a way to append to info as to add on as the loop progresses, still need to look that up though
    console.log(JSON.stringify(info, null, 1)); //Logs the body
    global.count = info.length; //Will return the value 10, until there are no more entries and then will terminate loop
  }
}

request(options, callback);//Sends request
}
//It just keeps running the loop and doesn't execute the request at the bottom which is what will make the loop terminate, I've tried many things with 
//callbacks and nothing has worked so far

我似乎无法能够使循环正常运行,我也不想求人,但我坚持我很伤心地说。提前谢谢。

1 个答案:

答案 0 :(得分:0)

我觉得问题中的一些混乱并清楚地解释了???

在你的循环写作中不断重复你想要的就是其他东西......

在我的想法中应该用于导航页面加载(每页包含10个)

global.count = 10;
for( var i = 1; i< =global.count; i++)
{
 -- Write Your code here ---
}