Help get this topic noticed by sharing it on Twitter, Facebook, or email.

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

/**
* 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);
}


usage example:


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





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


1 person likes
this idea
+1
Reply