hey people I'm having a little issue. I'm trying to pull some Flickr images from their api and for a reason I'm not aware of, the list is been populated not at the time I wish. I will post my code to explain better.
this is my code:
component:
export class MainComponent implements OnInit {
myArray: Item[] = [];
randomItem: Item;
constructor(private dataService: DataService) {
}
refresh(){
console.log("[refresh()]:",this.myArray.length)
var selected = Math.floor(Math.random() * this.myArray.length);
this.randomItem = this.myArray[selected];
this.myArray.splice(selected,1);
}
ngOnInit() {
this.dataService.getImages().subscribe(res => {
this.myArray = res.items;
console.log("[ngOnInit]:" ,this.myArray.length)
})
console.log("[ngOnInit 2]:",this.myArray.length)
if(this.myArray.length>0){
var myInterval = setInterval(this.refresh(), 1000);
}else{
clearInterval(myInterval);
}
}
}
Service:
export class DataService {
imagesUrl = 'https://api.flickr.com/services/feeds/photos_public.gne?format=json&nojsoncallback=1';
myArray:Item[]=[];
randomItem:Item;
constructor(private http: HttpClient) {
}
getImages(): Observable<Flickr>{
return this.http.get<Flickr>(this.imagesUrl);
}
}
output:
[ngOnInit 2]: 0
[ngOnInit 1]: 20
As you see in the component code, my Interval is never executed. looks like the array has no time to initialize itself. why its not working? what can I do to make that work?
that's not duplicated since the problem is the interval doesn't work either way.