0

I have a input field as filter. At every keyup he calls a function to post the input.val() to display the result in a table, this works fine. but if i'm typing a name "stackoverflow" he will post 13 times. I want that if he's getting no input for 3 sec that he posts.

I thought that there was a function for (not a default function). And I do not want to auto-complete.

HTML:

<input type="text" id="filter" />

JavaScript:

$(document).ready(function(){
    $("#filter").keyup(function(){
        console.log('POST');
    });
 });

jsfiddle

I don't wants a delay, because that will only "delays" it.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Mitchel Verschoof
  • 1,543
  • 4
  • 20
  • 38
  • You'll notice that if you include your code in your post you'll get help much sooner. – 11684 Jan 14 '13 at 13:46
  • "I don't have to do it"... yes you do, because we don't want to have to follow a link to understand the problem. Also, if the jsfiddle link is down for whatever reason, your question will be incomplete (I admit though that this is a relatively small example and your question might make sense even without it). The better your question, the better answers you get. That implies including code. – Felix Kling Jan 14 '13 at 13:49

2 Answers2

2

You can use the native Javascript functions clearTimeout and setTimeout to accomplish this.

$(document).ready(function(){
    var filterTimeout = null;
    $("#filter").keyup(function(){
        if (filterTimeout != null)
            window.clearTimeout(filterTimeout);
        filterTimeout = window.setTimeout(function() { console.log('POST'); }, 3000);
    });
 });
crush
  • 16,713
  • 9
  • 59
  • 100
0

Check out the jQuery throttle / debounce plugin

It does exactly what you want

$('input').keyup($.debounce(3000, function() { /* Post comes here */ }));
istvan.halmen
  • 3,320
  • 1
  • 23
  • 29