Wait for typing pause

Use this code to help reduce the number of UI updates as a user inputs a real time search string.

by DaveAlger

HTML

<b>Search:</b>
<input id="in" type="text" />
<div id="out"></div>

CSS

* { padding: 10px; font-family: sans-serif; }
#in { border: 2px solid #ccc; }

JavaScript

// update after 300 milliseconds of not typing
var doneTypingInterval = 300;
var typingTimer;

$(document).ready( function () {
    $('#in').focus();

    $('#in').keyup( function () {
        clearTimeout( typingTimer );
        typingTimer = setTimeout( function () {
            // do stuff here (ajax call, text parsing, etc.)
            var searchString = $("#in").val();
            $("#out").html(searchString);
        }, doneTypingInterval);
    });
});