How do I fake AJAX start / end events in JQuery?
<p>I'm listening to ajaxStart() and ajaxStop() to show/hide a spinner, and I'm doing some mock AJAX stuff in JS while servers are being written. It just calls a function to generate mock data with a setTimeout(). For now I'm just manually calling hide() and show() on the spinner, but I'd really like to just tell JQuery when I'm starting and stopping my "request", and have the events go through that way, so I don't accidentally hide() the spinner while a <em>real</em> ajax request is still going in the background.</p>

<p>Can this be done easily?</p>

<p><strong>EDIT:</strong> This is the code I settled on, the trick is maintaining the <code>JQuery.active</code> count:</p>

<pre><code>function fakeAJAX(f) {
    if(jQuery.active++ === 0 ) {
        jQuery.event.trigger("ajaxStart");
    }
    setTimeout(function () {
        f();
        if(!(--jQuery.active)) {
            jQuery.event.trigger("ajaxStop");
        }
    }, Math.round(Math.random() * 3000 + 250));
}
</code></pre>
