For the following code:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script>
$(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);
});
});
</script>
<div id="main">
<input name="code" />
</div>
Does anyone know why I’m getting the javascript error
Uncaught SyntaxError: Unexpected identifier
I am trying to delay sending keyup data to the server.
You’ve just forgotten to pass
delaya function definition. Just tweak your event handler slightly (everything else stays the same):Example: http://jsfiddle.net/f7F7c/