admin管理员组文章数量:1183727
Let's say I have some data that I need to get from the server about every 10 seconds. I would have a function that gets the data via AJAX and then call setTimeout to call this function again:
function GetData(){
$.ajax({
url: "data.json",
dataType: "json",
success: function(data){
// do somthing with the data
setTimeout(GetData, 10000);
},
error: function(){
setTimeout(GetData, 10000);
}
});
}
If someone leaves the web page open all day, it could get thousands of recursive function calls.
I don't want to use setInterval because that does not take into account network delay. If the network is busy and it takes 15 seconds to process the request, I don't want to ask it again before I get the AJAX timeout.
What is the best way to handle a function that needs to be called periodically?
Let's say I have some data that I need to get from the server about every 10 seconds. I would have a function that gets the data via AJAX and then call setTimeout to call this function again:
function GetData(){
$.ajax({
url: "data.json",
dataType: "json",
success: function(data){
// do somthing with the data
setTimeout(GetData, 10000);
},
error: function(){
setTimeout(GetData, 10000);
}
});
}
If someone leaves the web page open all day, it could get thousands of recursive function calls.
I don't want to use setInterval because that does not take into account network delay. If the network is busy and it takes 15 seconds to process the request, I don't want to ask it again before I get the AJAX timeout.
What is the best way to handle a function that needs to be called periodically?
Share Improve this question asked Jul 21, 2011 at 16:37 Robert DemlRobert Deml 12.5k21 gold badges69 silver badges92 bronze badges 3- Each subsequent call to GetData will be completely independent of each other. After any given GetData execution run completes, its context will be destroyed, and a new one created when the timer expires. – Marc B Commented Jul 21, 2011 at 16:42
- this would probably cause trouble for the server well before the client-side javascript. as the others have mentioned, you don't have a call stack issue because the setTimeout allows the function to return and be removed from the stack. Recursion algorithmically, but not actually. – aepheus Commented Jul 21, 2011 at 16:48
- This question is exactly what I am researching. Thanks for bring it here. – shaosh Commented Oct 13, 2015 at 6:55
1 Answer
Reset to default 34There's no actual recursion because the call to GetData is delayed and the JavaScript context is destroyed in the meantime. So it won't crash the JS engine.
For your code sample, this is basically what will happen at the JS engine level:
- Initialize JS engine
- Create GetData function context
- Execute GetData statements including "setTimeOut"
- "setTimeOut" instruct the JS engine to call a function in 10 seconds
- Destroy GetData function context
- At this point, in terms of memory use, we are back to step 1. The only difference is that the JS engine stored a reference to a function and when to call it (lets call this data "futureCall").
- After 10 seconds, repeat from step 2. "futureCall" is destroyed.
本文标签: javascriptWill a recursive 39setTimeout39 function call eventually kill the JS EngineStack Overflow
版权声明:本文标题:javascript - Will a recursive 'setTimeout' function call eventually kill the JS Engine? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738320087a2074454.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论