Skip to content

Instantly share code, notes, and snippets.

@ducas
Created March 21, 2014 04:39
Show Gist options
  • Save ducas/9679628 to your computer and use it in GitHub Desktop.
Save ducas/9679628 to your computer and use it in GitHub Desktop.
AJAX Loading Indicator
/* global baseUrl, alert, setTimeout */
define(
['jquery'],
function ($) {
var loadingCount = 0,
loadingMarkup = '<div id="loading-indicator" style="display:none;"><div></div><img src="/Content/Images/loading.gif" /></div>',
hiding = false,
showLoading = function () {
loadingCount++;
if (loadingCount !== 1) { return; }
if (hiding) { hiding = false; }
setTimeout(function () {
if (hiding) { return; }
$(loadingMarkup)
.appendTo('body')
.fadeIn('fast');
}, 100);
},
hideLoading = function () {
loadingCount--;
if (loadingCount !== 0) { return; }
hiding = true;
setTimeout(function () {
if (!hiding) { return; }
var indicator = $('body').find('#loading-indicator');
if (indicator.length === 0) { return; }
indicator
.fadeOut('slow')
.queue(function () {
indicator.remove();
});
}, 100);
};
$.ajaxPrefilter(function (options, originalOptions, jqXHR) {
showLoading();
jqXHR.always(hideLoading);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment