Please consider the following:
$(document).ready(function () {
var promise;
(function refresh(){
promise = loadInfo();
promise.done( function() {
$.each(loadedData, function(key, value){
$('#' + key + ' div.info').html(value.label);
})
// Call itself for next iteration
window.setTimeout(refresh, 5 *1000);
})
})()
})
- Do you think each iteration create a new var
promiseor they all reuse the same one? - In case a new var is created for each iteration, can I have an overload overtime (stack overflow!!!) ;) ? The application is displaying data and is supposed to run for long times
- I also have another version with
setInterval((function refresh(){...}), 5 *1000)without the setTimeout, which one is better? Thoughts?
Thank you
setTimeoutinside the loop is IMO preferable. Because, in case of long running indeterminate function call,setIntervalmay get overlap, whereassetTimeoutwill be called only when the call finishes and hence gives you a truer delay.