admin管理员组文章数量:1022761
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?
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?
1 Answer
Reset to default 3This code:
setTimeout(Art.generateSector(j*60),j*60000);
calls Art.generateSector
immediately, passing in j*60
, and then takes its return value and passes it to setTimeout
, exactly the way foo(bar())
calls bar
and passes its return value into foo
.
To schedule a call to the function, you pass in a function reference. In your case, you can probably use Function#bind
:
setTimeout(Art.generateSector.bind(Art, j*60),j*60000);
Function#bind
returns a new function that, when called, will call the original with the given value as this
(in our case, Art
) and any additional arguments you provide.
Function#bind
is an ES5 feature. If you have to support really old JavaScript engines like the one in IE8, this feature can be "shimmed" ("polyfilled"). Search for "function bind shim" or "function bind polyfill" to find multiple options.
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?
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?
1 Answer
Reset to default 3This code:
setTimeout(Art.generateSector(j*60),j*60000);
calls Art.generateSector
immediately, passing in j*60
, and then takes its return value and passes it to setTimeout
, exactly the way foo(bar())
calls bar
and passes its return value into foo
.
To schedule a call to the function, you pass in a function reference. In your case, you can probably use Function#bind
:
setTimeout(Art.generateSector.bind(Art, j*60),j*60000);
Function#bind
returns a new function that, when called, will call the original with the given value as this
(in our case, Art
) and any additional arguments you provide.
Function#bind
is an ES5 feature. If you have to support really old JavaScript engines like the one in IE8, this feature can be "shimmed" ("polyfilled"). Search for "function bind shim" or "function bind polyfill" to find multiple options.
本文标签: javascriptHow to prevent 429 (Too Many Requests) error using setTimeoutStack Overflow
版权声明:本文标题:javascript - How to prevent 429 (Too Many Requests) error using setTimeout - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745574947a2156961.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论