Search code examples
javascripthtmltoastsnackbar

Success or error toast after running function in Javascript


I have two functions which look like this:

function functionOne(par1, par2, par3) {
// this functions patches using an api, re-usable
}

function functionTwo(par2) {
functionOne(par1, par2, par3);
functionOne(par1, par2, par3);
functionOne(par1, par2, par3);
functionOne(par1, par2, par3);
}

This works as intended, but I want to give the user some feedback if he presses the button and functionTwo runs successfully or gives an error.

I found this: https://www.w3schools.com/howto/howto_js_snackbar.asp which is what I want, rather than a pop-up, but is it possible to display this after success or error and if so, how? Thanks!


Solution

  • Take a look at the following snippet:

    The HTML and CSS are the code extract from the W3C example. The JS function showToast() is based on their example but takes an optional parameter with a default value and appends that parameter value to the toast.

    I have made a mini example of an API (yourApiFuncton()) call to demonstrate the use.

    Of course this is only a basic example and you can work from there to get something fancier.

    function yourApiFunction(){
      
      //Do your api calls, the "error" variable is only set to demonstrate the use of the success/error message
      let error = false;
      
      if(error){
        showToast("Oh no...");
      }else{
          showToast("Hell yeah");
      }
    }
    
    function showToast(content = "Unknown error") { //You can change the default value
      // Get the snackbar DIV
      var x = document.getElementById("snackbar");
      
      //Change the text (not mandatory, but I think you might be willing to do it)
      x.innerHTML = content;
    
      // Add the "show" class to DIV
      x.className = "show";
    
      // After 3 seconds, remove the show class from DIV
      setTimeout(function(){ x.className = x.className.replace("show", ""); }, 3000);
    }
    
    //Simulating a call to your API
    yourApiFunction();
    /* The snackbar - position it at the bottom and in the middle of the screen */
    #snackbar {
      visibility: hidden; /* Hidden by default. Visible on click */
      min-width: 250px; /* Set a default minimum width */
      margin-left: -125px; /* Divide value of min-width by 2 */
      background-color: #333; /* Black background color */
      color: #fff; /* White text color */
      text-align: center; /* Centered text */
      border-radius: 2px; /* Rounded borders */
      padding: 16px; /* Padding */
      position: fixed; /* Sit on top of the screen */
      z-index: 1; /* Add a z-index if needed */
      left: 50%; /* Center the snackbar */
      bottom: 30px; /* 30px from the bottom */
    }
    
    /* Show the snackbar when clicking on a button (class added with JavaScript) */
    #snackbar.show {
      visibility: visible; /* Show the snackbar */
      /* Add animation: Take 0.5 seconds to fade in and out the snackbar.
      However, delay the fade out process for 2.5 seconds */
      -webkit-animation: fadein 0.5s, fadeout 0.5s 2.5s;
      animation: fadein 0.5s, fadeout 0.5s 2.5s;
    }
    
    /* Animations to fade the snackbar in and out */
    @-webkit-keyframes fadein {
      from {bottom: 0; opacity: 0;}
      to {bottom: 30px; opacity: 1;}
    }
    
    @keyframes fadein {
      from {bottom: 0; opacity: 0;}
      to {bottom: 30px; opacity: 1;}
    }
    
    @-webkit-keyframes fadeout {
      from {bottom: 30px; opacity: 1;}
      to {bottom: 0; opacity: 0;}
    }
    
    @keyframes fadeout {
      from {bottom: 30px; opacity: 1;}
      to {bottom: 0; opacity: 0;}
    }
    <!-- The actual snackbar -->
    <div id="snackbar">Some text some message..</div>