Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Capture the timeoutId in a closure.

  $(document).ready((function() {
    $('input').keypress((function() {
        var timeoutId = null;

        return function() {
            window.clearTimeout(timeoutId);
            timeoutId = window.setTimeout(function() {
                $.ajax({ /* ... * /});
            }, 200);
        };
    })());
  });

  // NB: this solution only works if there is one input.
  // If there are more than one, you need to create a new closure for each one.


Consider applying for YC's Winter 2027 batch! Applications are open till November 2.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: