This is how I do it. I usually add this to my _layout page so that these script and div are loaded on every page of my project. This will make Spinner show anytime there is ajaxSend, and hide on ajaxStop or ajaxError.
<script type="text/javascript"> $(document).ready(function () { BindSpinner(); }); function BindSpinner() { $("#spinner").bind("ajaxSend", function () { $(this).show(); }).bind("ajaxStop", function () { $(this).hide(); }).bind("ajaxError", function () { $(this).hide(); }); }; </script> <div id="spinner" class="spinner" style="display: none;"> <img id="img-spinner" src="@Url.Content("~/Content/Images/ajax-loader.gif")" alt="Loading..." /> </div>
Now, at any time, ajax makes the user interface wait for the counter to appear.
A guide to calling an action using ajax can be found here.
source share