var strokeTimeout = null; function instantSearch() { if (strokeTimeout) { clearTimeout(strokeTimeout); } strokeTimeout = setTimeout(doSearch, 150, value); } function doSearch() { value = document.getElementById("search").value; if (value === "") { strokeTimeout = null; return; } var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if (xhr.readyState == XMLHttpRequest.DONE) { if (xhr.status === 404 || xhr.status === 400 || typeof xhr.response === 'undefined' || xhr.response === '404 not found') { document.getElementById("search-results").innerHTML = ''; return } document.getElementById("search-results").innerHTML = xhr.response; } } xhr.open('GET', '/search/gril_instant/' + value, true); xhr.send(null); strokeTimeout = null; }