How can I use AJAX to check a file, like, every 30 seconds, and then just print the result? I would prefer to use jQuery, but just plain javascript is okay too.
P.S. I've been searching for how to do this for a while.
How can I use AJAX to check a file, like, every 30 seconds, and then just print the result? I would prefer to use jQuery, but just plain javascript is okay too.
P.S. I've been searching for how to do this for a while.
Use setInterval
to run a function every X milliseconds.
setInterval(function(){
$.get('file', function(data){ // Use AJAX to get the file you want
console.log(data); // Do something with the file
});
}, 30000); // 30000ms = 30s
Something like this should help:
function getData() {
$.get("filepath", function (data) {
// process data...
console.log(data);
// Invoke the data lookup again after 30s
window.setTimeout(function () {
getData();
}, 30000); // 30s
});
}
And then invoke the starting of the loop:
$(document).ready(function () {
getData();
});