How to disable a function temporarily in JavaScript?
<p>I am trying to disable a function in JavaScript so that it only can be called once every 300ms.</p>

<p>To do this, I guessed the best approach would be to do sth like this:</p>

<pre><code>var locked = (function () {
    var lock = null;
    return function () {
        if (lock != null)
            return false;

        lock = setTimeout(function () {
            lock = null;
        }, 300);
        return true;
    };
})();
</code></pre>

<p>And use this lock in other functions like this:</p>

<pre><code>function superImportantFunction() {
    if(locked()) return;
}
</code></pre>

<p>Now, this does not work for some reason. Do you have any better suggestions or maybe a reason why the code does not work?</p>

<p>Thanks!</p>
