Hacker News new | ask | show | jobs
by danielsoneg 5326 days ago
Just add a short delay between the keypress & the search/url push, and add a check to see if the query is the same. If after a ~second or so the query is the same, do the search and update the URL.
2 comments

Yes please! This was the first thing I noticed, maybe my netbook is too slow but I do a lot of coding on the little thing.

The way I solved this in a similar auto-complete app was using a setTimeOut to prevent updating or requesting data unless no key had been pressed in the last 0.2 seconds or so:

    var updateId = 0;
    function searchKeypress(e) {
      if ((e || window.event).keyCode == 13) {
        updateCookie(radioStations.selected);
        document.location = radioStations.selected.links[0].url;
      }
      clearTimeout(updateId);
      updateId = setTimeout(searchUpdate, 200);
    }
Disclaimer: this piece of code is about 4 years old and as you can see it doesn't even use jQuery, but I trust it demonstrates the principle well enough.
I'd use underscore.js's _.debounce method, simples!