function showToast(text, duration) { duration = duration || 3000; if (!$('#toast-container').length) { $('body').append('
'); } var $toast = $( '
' + '×' + '
' + '
' + '
' ); $toast.find('.toast-notif__body').text(text); $toast.find('.toast-notif__bar').css('animation-duration', duration + 'ms'); $('#toast-container').append($toast); function closeToast() { $toast.addClass('toast-notif--out'); setTimeout(function() { $toast.remove(); }, 320); } $toast.find('.toast-notif__close').on('click', closeToast); setTimeout(closeToast, duration); }