JSFiddle - React, Tailwind, and code Playground
HTML
<div id="top_notify">
<pre class="alert alert-success"></pre>
</div>
CSS
.alert-success {
color: green;
}
.alert-danger {
color: red;
}
JavaScript
$().ready(function() {
function handleBeforeSend(bool, jqxhr, settings) {
console.log(bool, jqxhr, settings);
if (bool === true) {
var alert_el = $('#top_notify').find('.alert');
// Remove any additional classes added by a possible previous run
alert_el.removeClass('visible alert-success alert-info alert-danger alert-warning');
}
}
function notifyComplete(type, alert_el, response) {
var msg = response.output;
var result = response.result;
// Update msg in alert box
alert_el.text(msg);
if (result == 'success') {
alert_el.addClass('alert-success');
} else {
alert_el.addClass('alert-danger');
}
// Slide in alert box
alert_el.addClass('visible');
}
function getAjaxData(loadUrl, dataObject, action, type, beforeSend, bool) {
return jQuery.ajax({
type: action,
url: loadUrl,
data: dataObject,
dataType: type,
beforeSend: beforeSend.bind(null, bool)
});
}
// pass `true` to `handleBeforeSend`
getAjaxData("/echo/json/", {
json: JSON.stringify({
"output": "123",
"result": "success"
})
}, "POST", "json", handleBeforeSend, true)
.then(function(response) {
notifyComplete(null, $('#top_notify'), response)
});
setTimeout(function() {
// pass `false` to `handleBeforeSend`
getAjaxData("/echo/json/", {
json: JSON.stringify({
"output": "123",
"result": "success"
})
}, "POST", "json", handleBeforeSend, false)
.then(function(response) {
notifyComplete(null, $('#top_notify'), response)
})
}, 5000)
})