i'm trying to create a new object as an attribute of a JSON for all positions of an array that contains JSON objects, but the problem is when run this, it always prints out the last and only the last object created inside for cycle. I can't figure this out... Can anybody explain me why this happens? Here is my code:
for(var i = 0; i < data.length; i++){
var clock = new Clock((datesService.parseToClock(data[i].endsAt) / 1000), data[i].id)
data[i].clock = clock
data[i].interval = setInterval(function(){
clock.decrement()
console.log('ID: ' +clock.getAuction() + ' | ' + clock.getDays()+' d '+ clock.getHours()+' h ' + clock.getMinutes() + ' m ' + clock.getSeconds())
}, 1000)
}
function Clock(tempo, auction){
this.auction = auction
this.tempo = tempo
this.seconds = this.tempo % 60
this.minutes = (this.tempo / 60) % 60
this.hours = ((this.tempo / 60) / 60) % 24
this.days = (((this.tempo / 60) / 60) / 24)
}
Clock.prototype.decrement = function(){
this.tempo --
this.seconds = Math.floor(this.tempo % 60)
this.minutes = Math.floor((this.tempo / 60) % 60)
this.hours = Math.floor(((this.tempo / 60) / 60) % 24)
this.days = Math.floor((((this.tempo / 60) / 60) / 24))
};
Clock.prototype.getAuction = function(){
return this.auction
};
Clock.prototype.getDays = function(){
return this.days
};
Clock.prototype.getHours = function(){
return this.hours
};
Clock.prototype.getMinutes = function(){
return this.minutes
};
Clock.prototype.getSeconds = function(){
return this.seconds;
};
Just can´t understand why... Thanks guys :)