1

So, right now I have an input form as a "search bar." On key up, it fetches some html from a page and displays it in a div. What I want to do is only fetch this html data IF more than 1 second has passed AFTER the key up. Otherwise, if another key was pressed before this 1 second, the timer will reset and start on the next key up.

So, like this

onkeyup -> start timer
once one second passes -> call getList function
onkeyup before one second passes, restart timer

Any ideas? Thanks.

  $('#searchbar')
.bind('keyup', function(e){
  document.getElementById("result").innerHTML = "";
  getList();
});

function getList() {
$.get("myaspscript", function(data, status) {
    $(result).html( data );
});
}
Harry
  • 772
  • 10
  • 32
  • Start a timer with `setTimeout()`, which returns a value. Save the value. If you want to cancel the timeout, pass that value to `clearTimeout()`. – Pointy Sep 15 '14 at 20:11
  • 2
    Here is a guide how you can achieve this : [MDN - setTimeout](https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers.setTimeout) , how to call a function after a period of time, how to interrupt it if the user has keypressed again, ect ... – KarelG Sep 15 '14 at 20:11
  • 2
    And please change your two results to $("#result").html(...) – mplungjan Sep 15 '14 at 20:12
  • @mplungjan when I change to "#result" I get Uncaught TypeError: Cannot set property 'innerHTML' of null – Harry Sep 15 '14 at 20:20

0 Answers0