Search code examples
angulartoastr

Limit max toastr messages to two


I want to limit the maximum number of open toasts at a time. I'm using ngx-toastr module and Angular5. I've configured global settings like so :

ToastrModule.forRoot(
      { maxOpened: 2, 
        preventDuplicates: true,
        timeOut:2000,
        closeButton: true,
        progressBar:true,
        autoDismiss:true,
        newestOnTop:true}),

and even cleared the toasts before rendering them :

for(let i=0; i<10;i++){
  this.toastr.clear();
  this.toastr.info(''+i);
}

but still, many toast messages are showing at once. enter image description here


Solution

  • I created a Stackblitz with a workaround to only allow two toasts at a time with a setTimeout of 1 second.

    for(let i=0; i<10;i++){
      ((ind)=>{ setTimeout(()=> this.toastr.info(''+i),1000*ind)})(i)
    }
    

    https://stackblitz.com/edit/angular-phf1p6