How to make a delay of execution of the asynchronous function that is called every second?

Good day!
How to make a delay of execution of asynchronous functions. When I add clearTimeout(timerId); function does not work, without the delay only works the first time. How to make swiderska would be every time?

const {setIntervalAsync} = require('set-interval-async/dynamic');
...
mounted(){
...
this.interval();
},
 methods: {
 interval: function () {
setIntervalAsync(
 async () => {
 await this.changeAlert();
},
1000
);
},
 changeAlert: async function () {
console.log(789);
 let timerId = setTimeout(() => {
 if (this.alertArr.length > 0) {
 this.alertCurrent = this.alertArr[0];
console.log(this.alertCurrent);
 } else {
 this.alertCurrent = null;
}
 this.alertArr.splice(0, 1);
 }, 5000);
clearTimeout(timerId);
},
}
 }
April 19th 20 at 12:41
1 answer
April 19th 20 at 12:43
in changeAlert you start the timer, which describe callback, and then immediately reset the timer and callback does not work. In other words You gave the car a bunch of instructions and then canceled them.
It is not clear what you want to achieve, maybe this will help You on the right idea ;)
changeAlert: function() {
console.log(789);
 return new Promise((resolve, reject)=>{
 let timerId = setTimeout(() => {
 if (this.alertArr.length > 0) {
 this.alertCurrent = this.alertArr[0];
console.log(this.alertCurrent);
 } else {
 this.alertCurrent = null;
}
 this.alertArr.splice(0, 1);
resolve()
 if (false) reject("error description");
 }, 5000);
})
}

Find more questions by tags Vue.jsJavaScript