I'm new in javascript. I wrote a function that sends an HTTP request to a site in a loop. I want to save the response time of each successful request to an array. but I get an error like this:
VM6905:6 Uncaught TypeError: TIMES is not a function at :6:4 (in line: k=TIMES(1))
In the end, I want to have a code that calculates maximum response time of all successful requests and if the difference between this time until now is less than 500 ms, wait until this time elapses and sends the request. otherwise, just send the request again.
and this is my code:
function senderFUNCTION(k){
TIMES=new Array(2).fill(0);
var start_time = new Date().getTime();
var xhr = new XMLHttpRequest();
xhr.open("POST", 'https://***', true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
var json = JSON.parse(xhr.responseText);
var request_time = new Date().getTime();
k=k+1;
return TIMES=[request_time,k]
}
};
xhr.send(data);
}
a=new Array(5).fill(0);
var i;
var k=-1;
for (i=0;i<5;i++){
var TIMES=senderFUNCTION(k)
k=TIMES(1);
a[k]=TIMES(0);
}