JavaScript Alert with Timer


How can I create an Alert in JavaScript that shows for 2 seconds and then closes automatically (only) without the user clicking ok?


I present to you Toastr , it is a JavaScript library that generates notifications in a " modern " way:

toastr.options = {
  "debug": false,
  "positionClass": "toast-bottom-right",
  "onclick": null,
  "fadeIn": 300,
  "fadeOut": 100,
  "timeOut": 5000,
  "extendedTimeOut": 1000

var showToastrs = false;

function toastrs() {
  if (!showToastrs) {
    toastr.error('Estamos bajo ataque DDoS', 'Error Critico!');
    toastr.success('Se guardaron los cambios satisfactoriamente', 'Todo en orden');
    toastr.warning('La latencia del server esta aumentando.', 'Alerta!');
  } else {
    toastr.error('no se puede!\'t.', 'Otro error crítico');

// Definimos los callback cuando el TOAST le da un fade in/out:
toastr.options.onFadeIn = function() {
  showToastrs = true;
toastr.options.onFadeOut = function() {
  showToastrs = false;

$(function() {
  $("#clear").on("click", function() {
    // Clears the current list of toasts
  $("#rewind").on("click", function() {
    // show toastrs :)
<script src=""></script>
<script src=""></script>
<link href=""/>
<link href=""/>
<a href="" target="_blank">Toastr official demo</a>
<input id="clear" type="submit" value="Limpiar notificaciones">
<input id="rewind" type="submit" value="Mostrar notificaciones">
Scroll to Top