Created
March 21, 2014 04:39
-
-
Save ducas/9679628 to your computer and use it in GitHub Desktop.
AJAX Loading Indicator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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