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?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…