Running a task in the background using javascript
<p>I have a widget that displays a "waiting" image whenever a user submits a form, implemented with the following code:</p>

<pre><code>&lt;div id="shading"&gt;&lt;/div&gt;
&lt;div id="spinner-wrapper"&gt;
    &lt;div id="spinner-target"&gt;&lt;/div&gt;
&lt;/div&gt;


$("form").submit(function() {
    $("#spinner-wrapper").css({"visibility": "visible", "display": "block" });
    $("#shading").css({"z-index": "97"});
    var target = document.getElementById('spinner-target');
    var spinner = new Spinner(opts).spin(target);       
});         
</code></pre>

<p>This has the desired effect of graying out the screen, showing the spinner, and then when the form is done submitting, the user is directed to the target page.</p>

<p>However, sometimes the form is being submitted such that there is no redirect - specifically, if the user selects that they want to download a file, I return the file itself, and kill the process to block the redirect from occurring. When this happens, the spinner remains in the foreground, blocking the user from interacting with the site - not the desired behavior.</p>

<p>How could I somehow say that after X number of seconds (considering the target usage of the application, this would suffice), to stop/hide the spinner - that is, allow the form to be submitted, then show the spinner, then hide it (while this function does that in a different order - shows the spinner, then submits the form)?</p>
