Answer the question
In order to leave comments, you need to log in
How to add an element to an array through a function?
Good afternoon.
I need to add an element to the array after a certain time, I'm not strong in typescript.
Please, help.
i have this code
import { Component} from '@angular/core';
@Component({
selector: 'mail-box',
templateUrl: 'mailBox.component.html',
styleUrls: ['./style.css']
})
export class mailBoxComponent {
items = [1];
}
function add(i: number) {
if (i <= 10) {
addItem();
setTimeout(add, 3000);
}
}
function addItem() {
this.items.leght + 1;
}
Answer the question
In order to leave comments, you need to log in
I'm not good at typescript
let items = [1];
add = (i) => {
if (i <= 10) {
setTimeout(() => addItem(i), 3000)
}
}
addItem = (item) => {
items = [...items, item]
//items = items.concat([item])
//items.push(item)
}
//проверить
test = () => {
console.log(items)
add(5)
add(6)
add(999)
console.log(items)
setTimeout(() => console.log(items), 3500)
}
test()
//Console:
//[1]
//[1]
//[1, 5, 6]
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question