I am trying to make a program in node.js that reads a text file line by line and shows each of them with a delay of 2 seconds. The code I am testing is the following.
var fs = require('fs'),
readline = require('readline');
var FileSystem_Lectura=function()
{
this._rd=null;
}
FileSystem_Lectura.prototype.abrirArchivoCSV=function(nombreArchivo)
{
this._rd = readline.createInterface
({
input: fs.createReadStream(nombreArchivo),
output: process.stdout,
terminal: false
});
}
FileSystem_Lectura.prototype.leerArchivoCSV=function()
{
self=this;
this._rd.on('line',self.mostraLineasDelay);
}
FileSystem_Lectura.prototype.mostraLineasDelay=function(linea)
{
setTimeout(self.mostraLinea,20000,linea);
}
FileSystem_Lectura.prototype.mostraLinea=function(linea)
{
console.log("Linea:"+ linea);
}
var FS =new FileSystem_Lectura();
FS.abrirArchivoCSV(process.argv[2]);
FS.leerArchivoCSV();
The problem is that settimeout shows me all the lines together, it does not apply the delay. Except for the first line. So, how can I make it work properly?
From already thank you very much