Semaphore, run_once decorator, or something like it
<p>In some places we have to have only one instance of function running. </p>

<p>This code works for me: </p>

<pre><code>function example() { 
    var that = this; 
    if(that.running) { 
       return false; 
    }
    that.running = true; 
    $.get(url, {}, function (data) {
         that.running = false; 
    }); 
}
</code></pre>

<p>How we can improve it, and make it more reusable? </p>

<p><strong>UPD</strong> Here is solution, based on Frits van Campen answer:</p>

<pre><code>function make_run_once(callback) {
    callback.running = false;
    return function () {
        if(callback.running) {
            return false;
        }
        callback.running = true;
        deferred = $.Deferred();
        deferred.done(function () {
            callback.running = false;
        });
        callback(deferred);  // pass deferred to callback so it can resolve at it's own leisure
    };
}
</code></pre>
