sorry for the title, I was trying to be specific but it's a bit confusing. Actually the piece of code should be the better way to show you what my problem is:
var i = 0
var jsonArray = []
var startTime = new Date().getTime()
var writeFile = setInterval(function () {
var currentTime = new Date().getTime()
var stopTime = 31 * 1000
var executeScript = setInterval(function () {
// the script will stop after some time
if (currentTime - startTime > stopTime) { // time in ms: 60 * 60 * 1000 = 1 hour
process.exit()
}
// making request and fetching data
http.get(request, function (response) {
response.pipe(bl(function (err, data) {
if (err) return console.error(err)
response.setEncoding('utf8')
parseString(data, function (err, result) {
result = result.SoccerFeed.SoccerDocument[0].MatchData[0].TeamData
// creating object and filling it with received data
result = createJsObject(result)
// converting object in json format
var jsonObject = JSON.stringify(result, null, 4)
jsonArray.push(jsonObject)
// copying json into a new file without overwriting previous written data
})
}))
}).on('error', console.error)
}, 1 * 1000) // interval in ms: 90 * 1000 = 90 seconds / 1 min 30
fs.writeFile('API - Opta - ' + moment().format('YYYY - DD.MM (HH:mm:ss)') + '.txt', jsonArray, function(err) {
if (err) throw err
})
jsonArray = []
i++
}, 10 * 1000)
The problem is that the array I'm pushing in the file is keeping data from the old one, even when I clean it with jsonArray = []. I know I'm doing something wrong but I don't know what.
Thanks in advance for your help