Skip to content

Instantly share code, notes, and snippets.

@gregrickaby
Last active January 24, 2017 07:57
Show Gist options
  • Save gregrickaby/6529091 to your computer and use it in GitHub Desktop.
Save gregrickaby/6529091 to your computer and use it in GitHub Desktop.
Simple jQuery Ajax Modal with fail-safe.
/**
* Loading Modal
*/
$(body).on({
// When ajaxStart is fired, add 'loading' to body class
ajaxStart: function() {
$(this).addClass('modal-loading');
// After 6 seconds, change button text
timer = setTimeout(function() {
$('.nav-previous a').html("Still Loading...");
}, 6000);
// After 12 seconds, die. Reload page from scratch
timer = setTimeout(function() {
$(this).removeClass('modal-loading');
$('.nav-previous a').html("Error has occured. Reloading page.");
window.location.reload();
}, 12000);
},
// When ajaxStop is fired, clear the timer and remove 'loading' from body class
ajaxStop: function() {
clearTimeout(timer);
$(this).removeClass('modal-loading');
}
});
/* Loading Modal
-------------------------------------------- */
.modal {
background: url('images/ajax-loader.gif') 50% 50% no-repeat;
display: none;
height: 32px;
}
body.modal-loading {
overflow: hidden;
}
body.modal-loading .modal {
display: block;
}
<div class="modal"></div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment