Restrict the maximum toast to show in EJ2 TypeScript Toast control

10 May 20233 minutes to read

You can restrict the maximum toast count by using the event callback function and terminate the toast displaying process by setting the cancel event property in the beforeOpen event.

The following sample demonstrates restricting toast displaying up to 3. You can restrict by your own count with custom code blocks.

import {Toast, ToastModel} from '@syncfusion/ej2-notifications';
import { enableRipple } from '@syncfusion/ej2-base';


let maxCount: number = 3;
let toasts: ToastModel[] = [
    { title: 'Warning !', content: 'There was a problem with your network connection.' },
    { title: 'Success !', content: 'Your message has been sent successfully.' },
    { title: 'Error !', content: 'A problem has been occurred while submitting your data.' },
    { title: 'Information !', content: 'Please read the comments carefully.' }
let toastFlag: number = 0;

let toast: Toast = new Toast({
    title: 'Sample Toast Title',
    content: 'Sample Toast content'
    position: { X: 'Right', Y: 'Bottom' }
    beforeOpen: onBeforeOpen

document.getElementById('show_toast').onclick = () => {[toastFlag]);
  if (toastFlag === (toasts.length)) {
      toastFlag = 0;

function onBeforeOpen(e: ToastBeforeOpenArgs): void {
  if (maxCount === toast.element.childElementCount) {
   e.cancel =true;
  } else {
    e.cancel = false;
<!DOCTYPE html>
<html lang="en">

    <title>Essential JS 2 Toast</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="Typescript Toolbar Controls" />
    <meta name="author" content="Syncfusion" />
    <link href="index.css" rel="stylesheet" />
    <link href="" rel="stylesheet" />
    <script src=""></script>
    <script src="systemjs.config.js"></script>
<script src="" type ="text/javascript"></script>

    <div id='loader'>LOADING....</div>
    <div id='container'>
        <div class='row'> <button class="e-btn" id="show_toast"> Show Toast</button> </div>
        <div id='element'></div>
        <div id='result'></div>
        <div id='templateToast' style="display: none;color:red"> System affected by virus !!! </div>