I have an Ajax request that extracts data from a JSON object created in MongoDB and shows them to me on the screen. But what I'm stuck in is showing a word, from that array, randomly every 5 seconds. I have made the setInterval and it works, but I have tried to do a math.random to the words and nothing, I do not get it. I leave the code of my component.ts
export class FiveWordsComponent implements OnInit {
public words: Word[];
constructor(
private _wordService: WordService
) { }
ngOnInit() {
setInterval(() => {
this.getWords();
}, 5000);
}
getWords(){
this._wordService.getWords().subscribe(
response => {
if(response.word){
this.words=response.word[0].words;
}
},
error => {
console.log(<any>error);
}
);
}
}
And now I leave you my .html
<!DOCTYPE html>
<html lang="es" dir="ltr">
<head>
<meta charset="utf-8">
<title>Cinco palabras</title>
</head>
<body>
<app-header></app-header>
<section id="content">
<ul>
<li *ngFor="let word of words" class="list-words">{{word}}</li>
</ul>
<!-- audio -->
<!-- <app-audio></app-audio> -->
</section>
<app-footer></app-footer>
</body>
</html>
I attach the JSON, the data is for proof.
{
"_id" : ObjectId("5beeb5fb822c52a42c7c93da"),
"words" : [
"hola",
"adsfsd",
"gf",
"wrr",
"kj",
"klj",
"lkj",
"kj",
"klj",
"kj",
"klj",
"kj",
"klj",
"kj",
"klj"
],
"images" : [],
"thematics" : [],
"terminations" : [],
"histories" : []
}
Thank you very much.
Greetings