I'm triggering a function on setInterval()
to basically do an HTTP GET
calls every 30s however every time it triggers it duplicates the data as they're being pushed in an array.
The idea of the for()
is to find every objects
with status of AwaitingPicking and AwaitingCollection
then push them to their respective array
.
private awaitingPicking: any = [];
private awaitingCollection: any = [];
constructor(private sharedService: SharedService, private http: Http) {
setInterval(() => {
this.getOrdersFromOrigin();
}, 10000);
}
getOrdersFromOrigin() {
this.isLoading = true;
this.sharedService.getOriginSite()
.subscribe(data => {
this.isLoading = false;
for (var i = 0; i < data.Results.length; i++) {
var element = data.Results[i];
if (element.OrderLineGroups[0] && element.OrderLineGroups[0].Status == 'AwaitingPicking') {
this.awaitingPicking.push(element);
} else if (element.OrderLineGroups[0] && element.OrderLineGroups[0].Status == "AwaitingCollection") {
this.awaitingCollection.push(element);
}
}
},
err => {
this.isLoading = false;
});
}
Is there a way to prevent the data from duplicating every time the setInterval()
triggers?
I tried something like the code below but it still duplicating the element.
if(this.awaitingPicking.indexOf(element) === -1){
this.awaitingPicking.push(element);
}