Reputation: 37
I am using following code to read data from a text log file which is continually growing.
setInterval(call, 1000);
function call() {
$.ajax({
type: "GET",
url: "GenNumber.txt",
dataType: "text",
success: function(data) {
readdata(data);
},
});
}
My query is , does ajax reads complete file in every call or the (new) added lines from last call. If it reads entire file in each call, it may crash when file size bigger and bigger. Is there any way to read only new lines from the last call and stop the program when file stops growing.
Thanks
Upvotes: 1
Views: 3876
Reputation: 4094
You can use the headers
parameter for the settings
parameter to the ajax() function to set a Range
HTTP header to limit what data you are receiving.
Some examples:
bytes=9500-
will start sending data at offset 9500.
bytes=-500
will send the last 500 bytes. This requires that the HTTP server supports ranges (most do).
The Range header is documented in RFC 7233.
Taking your example code:
$.ajax({
type: "GET",
url: "GenNumber.txt",
dataType: "text",
success: function (data) { readdata(data); },
headers: {
"Range" : "bytes=-500"
}
});
Upvotes: 3
Reputation: 3919
In complement to the neuhaus answer, using the Range
header, you can store from the client side the total bytes already read.
So at each request, you will receive only data appended since the last call.
var byteRead=0;
setInterval(function(){
$.ajax({
type: "GET",
url: "GenNumber.txt",
dataType: "text",
success: function (data) {
byteRead+= data.length;
readdata(data);
},
headers: {
"Range" : "bytes="+byteRead+"-"
}
});
},1000);
Upvotes: 1