I am using a service to replace the words of an article with synonyms, the API of the service has a limit of 60 requests per minute. I have two functions, the first one get the article and split it into an Array, then calls the other to replace the words, I tried to do that by setting timeout to the second so it will be called first and then after 60 seconds, and then after 120 secs... so every minute I will call the service at most 60 times.
generateArticle : function(data){
Art.words = data.split( );
for(var j=0; j<Art.words.length/60; j+=1){
setTimeout(Art.generateSector(j*60),j*60000);
}
},
generateSector : function(position){
var count = 0;
for(var i=position; i<Art.words.length; i+=1){
if(Art.words[i].length > 3 && isNaN(Art.words[i]) && count < 60){
Art.findsimilarword(Art.words[i],i);
count++;
}
}
},
but what is happening is that the second function is called immediately, so in an article with 400 words the first 60 words will be replaced correctly but for the rest 340 words I am getting an error 429 (Too Many Requests)
. Am I using the setTimeout with a wrong way? Can someone explain to me why this is happening?