I am trying to better understand the use of deferred objects in jQuery.
The getData
method below fetches some data asynchronously. When done, it should be displayed to some predefined (log
) section by addToChart
. This should happen periodically, hence my using setInterval
inside getData
's done
handler.
function getData() {
return $.ajax({
url: 'https://demo-live-data.highcharts.com/time-data.csv',
type: 'GET'
});
}
getData().done(addToChart, function() {
setInterval(getData, 1000);
});
function addToChart(data) {
document.getElementById('log').innerText += data;
}
$(document).ready(function() {
getData();
});
In above code, getData
seems to get called only once. How do I let it be called periodically?
Also, is there any way to actually debug this code, rather than run it and scratch my head why it doesn't behave as expected? (I'm new to JavaSCript, in case you wonder). I stepped through the code using the Firefox debugger but that didn't help.