V
V
veryoriginalnickname2021-08-30 21:22:09
Vue.js
veryoriginalnickname, 2021-08-30 21:22:09

Why are notifications crookedly removed?

I am making a notification plugin, and the essence is this:
1. The function for adding a notification is called
2. The notification is added to the array
3. Vue renders an array with notifications, causing a timeout along the way
4. After the time expires, the notification deletes itself
Wrote the code, but for some reason it works crookedly. At first, notifications are deleted like normal, but then they start to be deleted in a scatter and in batches. Why?
Code:
https://codesandbox.io/s/eloquent-pasteur-xhzge?fi...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
veryoriginalnickname, 2021-08-30
@veryoriginalnickname

it was necessary to add timeout id and check it:

setNotification(notification){
      const _instance = this
      this.notifications.push({id: this.notificationsID, timeoutID: undefined, self: function () {
          if(this.timeoutID){
            return notification
          }
          this.timeoutID = setTimeout(() =>{
            const index = _instance.notifications.findIndex(obj => obj.id === this.id)
            _instance.notifications.splice(index, 1)
          }, 5000)
          console.log('timeout')
        return notification
        }})
      this.notificationsID++
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question