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);
},
}
}
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);
})
}
data: () => ({
interval: 0,
counter: 0
}),
mounted () {
this.start()
},
methods: {
start () {
this.counter = 0
clearInterval(this.interval)
this.interval = setInterval( () => {
this.counter++
console.log(this.counter)
}, 1000)
},
stop () {
this.counter = 0
clearInterval(this.interval)
}
}