How do I delay ajax calls to the server in javascript?
<p>For the following code:</p>

<pre><code>&lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"&gt;&lt;/script&gt;
&lt;script&gt;
$(document).ready(function() {
    var delay = (function() {
        var timer = 0;
        return function(callback, ms) {
            clearTimeout(timer);
            timer = setTimeout(callback, ms);
        };
    })();

    $("div#main").on("keyup", "input[name=code]", function() {
        delay($.getJSON("/some-url/", function(data) {
            console.log("here");
        }), 2000);
    });

});
&lt;/script&gt;

&lt;div id="main"&gt;
  &lt;input name="code" /&gt;
&lt;/div&gt;
</code></pre>

<p>Does anyone know why I'm getting the javascript error</p>

<pre><code>Uncaught SyntaxError: Unexpected identifier
</code></pre>

<p>I am trying to delay sending keyup data to the server.</p>
