0

so I recently started learning coding, and I wanted to know how I can multiply/loop this request instead of it being sent only once.

request({
    url: URL,
    method: 'GET',
    json: true
}, function (error, response, body){
    if(error){
        console.log("Error!")
    } else if(!error && response.statusCode == 200){
        console.log(chalk.green('Entered successfuly!'))
    }
})
4
  • 2
    what do you mean by "multiply/loop". When/how should be sent another request? You probably don't just want to DOS your own server. Commented Mar 23, 2019 at 19:32
  • Why would you want to loop requests? Recursion will take place and like Thomas said, DDoS attack. You would likely want to make a single request and loop through the data. Please update question with more information and code please. Good luck! Commented Mar 23, 2019 at 19:32
  • It's a request to a page where I want to get more views on, each request equals 1 view, I want to loop it so I don't have to restart the code over and over. Not sure if I explained it well, sorry. Commented Mar 23, 2019 at 19:35
  • so you want to scrape another page, imo. here's a good explanation medium.freecodecamp.org/… Commented Mar 23, 2019 at 19:39

2 Answers 2

1

The request can be sent many times over time using a setTimeout loop:

function sendRequest() {

  setTimeout(function() {

    $.ajax({
        url: 'http://localhost/example',
        method: 'GET',
        json: true
    }, function (error, response, body){
        if(error){
            console.log("Error!")
        } else if(!error && response.statusCode == 200){
            console.log(chalk.green('Entered successfuly!'))
        }
    });

    sendRequest();

  }, 1000);

}

sendRequest();

Or as an interval function:

function sendRequest() {

  $.ajax({
      url: 'http://localhost/example',
      method: 'GET',
      json: true
  }, function (error, response, body){
      if(error){
         console.log("Error!")
      } else if(!error && response.statusCode == 200){
          console.log(chalk.green('Entered successfuly!'))
      }
  });

}

let interval = setInterval(sendRequest, 1000);

If you'd like to send the request a fixed number of times, the first function can be modified like that:

function sendRequest(i) {

  if (i > 0) {

    setTimeout(function() {

      $.ajax({
          url: 'http://localhost/example',
          method: 'GET',
          json: true
      }, function (error, response, body){
          if(error){
              console.log("Error!")
          } else if(!error && response.statusCode == 200){
              console.log(chalk.green('Entered successfuly!'))
          }
      });

      sendRequest(i - 1);

    }, 1000);

  }

}

sendRequest(3);
Sign up to request clarification or add additional context in comments.

Comments

0

A loop will call the function as fast as your computer can process it. Instead put it into a timeout that calls itself so you can regulate the requests.

function makeRequest(){
 request({
    url: URL,
    method: 'GET',
    json: true
 }, function (error, response, body){
    if(error){
        console.log("Error!")
    } else if(!error && response.statusCode == 200){
        console.log(chalk.green('Entered successfuly!'))
    }
 })
 setTimeout(makeRequest,1000)
}
makeRequest()

1 Comment

Thank you a lot! That's what I was looking for

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.