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