Using CSV data, I am trying to create a live clock. I read data like this,
$(document).ready(function() {
$.ajax({
type: "GET",
url:"d741a.csv",
dataType: "text",
success: function(data) {processData(data);}
});
});
function processData(allText) {
var allTextLines = allText.split(/\r\n|\n/);
var headers = allTextLines[0].split(',');
const result = allTextLines.slice(1).reduce((sum, arr) => {
arr.split(',').forEach((v, i) => {
sum[i] = (sum[i] || 0) + Number(v.trim());
})
return sum;}, []);
//console.log(result[4]/36035);
var lines = [];
for (var i=1; i<allTextLines.length; i++) {
var data = allTextLines[i].split(',');
if (data.length == headers.length) {
var tarr = [];
for (var j=0; j<headers.length; j++) {
tarr.push(data[j]);
}
//lines.push(tarr);
}
}
...calculate numbers...
var date2 = new Date(final_year, final_month-1, final_day, 00, 00, 00);
return date2
}
In this function, I read data, calculate the numbers and create the date. And I connect this to the live clock as follows:
function startClock(){
var last_date = processData("d741a.csv").getTime();
//console.log(last_date);
....Code
document.getElementById('txt').innerHTML =
" "+year+" Year "+ day +" Day "+ hour +":"+ minute +":"+second;
setTimeout(startClock, 1000);
}
But when I run it, it calculates numbers only once. Otherwise, I get NaN value and see NaN : NaN : NaN for the clock. Why it works only once? What should I fix?
Example of error: NaN values