Terry Gilliver
Posts: 0
Joined: Fri Apr 18, 2014 8:45 pm

alert boxes with a timeout (no need to click ok)

I have small javascript functions, which I think may be useful to others: I use these regularly in my service call events

pre/**

  • Add a timeout alert function
    *
    */
    function Alert(msg,duration=3000)
    {
    var el = document.createElement("div");
    el.setAttribute("style", "position:fixed; top:10%; left:10%; border: black 1px solid; background-color:white; color:green; width:80%; text-align: center; z-index: 10;");
    el.innerHTML = "
    " + msg + "
    ";
    setTimeout(function(){
    el.parentNode.removeChild(el);
    },duration);
    document.body.appendChild(el);
    }

    /**

  • Echo Error Message for example on service failure
    */
    function alertError(msg, duration=3000) {
    var el = document.createElement("div");
    el.setAttribute("style", "position:fixed; top:10%; left:10%; border: black 1px solid; background-color:white; color:red; width:80%; text-align: center; z-index: 10;");
    el.innerHTML = "
    " + msg + "
    ";
    setTimeout(function(){
    el.parentNode.removeChild(el);
    },duration);
    document.body.appendChild(el);
    }/pre

    usage example:

    pre
    /**

  • popup message for 4 seconds (default is 3 seconds)
    */
    Alert("Service Executed Successfully",4000);
    /pre

    Image

    pre
    /**

  • popup error message for 3 seconds (default)
    */
    alertError("Service Execution Failed");
    /pre

    Image

Return to “Issues”