612

I want to trigger an ajax request when the user has finished typing in a text box. I don't want it to run the function on every time the user types a letter because that would result in A LOT of ajax requests, however I don't want them to have to hit the enter button either.

Is there a way so I can detect when the user has finished typing and then do the ajax request?

Using jQuery here!

Youssouf Oumar
  • 29,373
  • 11
  • 46
  • 65
David Zorychta
  • 13,039
  • 6
  • 45
  • 81
  • 27
    I think you'll need to define "finish typing" for us. – Surreal Dreams Nov 18 '10 at 22:14
  • 8
    While @Surreal Dreams' answer satisfies most of your requirements, if the user starts typing again AFTER the specified timeout, multiple requests will be sent to the server. See my answer below which stores each XHR request in a variable and cancels it before firing off a new one. This is actually what Google does in their **Instant** search. – Marko Nov 18 '10 at 22:33
  • The chosen answer is incorrect for a few reasons: 1. It always fires after 5 seconds even if user is typing. 2. It doesn't wait until user has finished typing as requested. 3. It fires multiple requests as mentioned by @Marko above. See my corrected answer below. – going May 08 '11 at 10:28
  • 1
    What about blur? I guess the user has definitely finished typing when the input element loses focus. – Don P Aug 31 '16 at 19:54
  • 11
    A simple google search could've gotten you the simple answer: https://schier.co/blog/2014/12/08/wait-for-user-to-stop-typing-using-javascript.html – Cannicide Feb 22 '18 at 00:54

34 Answers34

816

So, I'm going to guess finish typing means you just stop for a while, say 5 seconds. So with that in mind, let's start a timer when the user releases a key and clear it when they press one. I decided the input in question will be #myInput.

Making a few assumptions...

//setup before functions
var typingTimer;                //timer identifier
var doneTypingInterval = 5000;  //time in ms, 5 seconds for example
var $input = $('#myInput');

//on keyup, start the countdown
$input.on('keyup', function () {
  clearTimeout(typingTimer);
  typingTimer = setTimeout(doneTyping, doneTypingInterval);
});

//on keydown, clear the countdown 
$input.on('keydown', function () {
  clearTimeout(typingTimer);
});

//user is "finished typing," do something
function doneTyping () {
  //do something
}
Surreal Dreams
  • 26,055
  • 3
  • 46
  • 61
  • 4
    Thanks :) I started typing my comment on the question and realized I had a decent idea. – Surreal Dreams Nov 18 '10 at 22:22
  • Sorry to be a pain in the @ss, but you've got keyup twice instead of keydown on the 2nd event, and textbox isn't a valid selector since there isn't a `textbox` element :) – Marko Nov 18 '10 at 22:35
  • you have `keyup` typo instead of `keydown` – fearofawhackplanet Nov 18 '10 at 22:36
  • Thanks for the notes on the typo, I fixed it. I also updated the selector to be a little more real-world. – Surreal Dreams Nov 18 '10 at 23:09
  • 40
    This answer does not work correctly, it will always fire after 5 seconds unless the user types very slowly. See working solution below. – going May 08 '11 at 10:07
  • 4
    If you run into trouble here, because the timer fires immediately, try to add quotes around the function call: setTimeout('functionToBeCalled', doneTypingInterval); – Largo Jul 13 '12 at 10:08
  • 2
    Doesn't work when you copy/paste data into the field. – Jessica Jan 20 '14 at 18:56
  • @Largo setTimeout() should not be called with a string as the first parameter. Doing so is equivalent to using eval(). – Scottux Jan 28 '14 at 20:30
  • @Macmee - I went ahead and incorporated your suggested edit - I think it's a valid improvement. Please feel free to add any further comment. – Surreal Dreams Feb 20 '14 at 16:22
  • 1
    Shouldn't this be `typingTimer = setTimeout(function() {doneTyping(); }, doneTypingInterval);` Just curious. The above mentioned answer didn't work for me :/ – Anant Gupta Apr 17 '15 at 21:07
  • 4
    I'm seeing a few comments where the callback function, `doneTyping` in the example, is running immediately. That's usually a result of accidentally including `()` after the function name. Note that it should read `setTimeout(doneTyping,` not `setTimeout(doneTyping(),` – Anthony DiSanti Jun 19 '15 at 23:29
  • @SurrealDreams Hi dear, i have tested your answer for my question but it is not working. If you have a time can you check my [QUESTION](http://stackoverflow.com/questions/33974414/ajax-user-typing-message-for-chat) for me ? –  Dec 06 '15 at 11:05
  • 2
    @Jessica to work with paste you have to add 'paste' event like this: on('keyup paste', – Sergey Apr 01 '16 at 13:17
  • @Sergey - thanks for the comment, that's really useful. – Surreal Dreams Apr 05 '16 at 14:55
  • 2
    I scrapped the separate `keydown` function and just added it to the first function. I also added change in there. So it reads like this `twitterUsernameField.on('keyup keydown change', function () {` and it appears to work fine... – Luke May 15 '16 at 22:10
  • can i use this in my production ? :D – colin rickels Jan 30 '17 at 14:57
  • @colinrickels - go for it. I hope you find it useful. – Surreal Dreams Jan 30 '17 at 16:01
  • Here is the code above jsfiddle.net/matt_doran/g0rrj540/4 – Matt Doran Jun 22 '17 at 09:46
  • worked for me from copy-paste, i just changed my element on this code `var $input = $('#myInput');` – kapitan Feb 02 '19 at 13:35
  • Is there a way we can pass the keyup `event` to `doneTyping` function without triggering it immediately? – Ye' Thura Ag Oct 27 '20 at 19:58
  • instead of timing, one should consider the string length in the search bar! user can write only 1 character and wait for 5 seconds so better to check string size. – Vishal Nov 09 '20 at 08:09
  • 1
    I was trying to build a similar function and was apparently way over complicating it. This helped me hash our what I was doing wrong. Thanks! – Bilbonic Jan 23 '21 at 20:22
  • very very very very high quality @SurrealDreams thanks – Daniel Garcia Sanchez Mar 21 '23 at 14:59
479

The chosen answer above does not work.

Because typingTimer is occassionaly set multiple times (keyup pressed twice before keydown is triggered for fast typers etc.) then it doesn't clear properly.

The solution below solves this problem and will call X seconds after finished as the OP requested. It also no longer requires the redundant keydown function. I have also added a check so that your function call won't happen if your input is empty.

//setup before functions
var typingTimer;                //timer identifier
var doneTypingInterval = 5000;  //time in ms (5 seconds)

//on keyup, start the countdown
$('#myInput').keyup(function(){
    clearTimeout(typingTimer);
    if ($('#myInput').val()) {
        typingTimer = setTimeout(doneTyping, doneTypingInterval);
    }
});

//user is "finished typing," do something
function doneTyping () {
    //do something
}

And the same code in vanilla JavaScript solution:

//setup before functions
let typingTimer;                //timer identifier
let doneTypingInterval = 5000;  //time in ms (5 seconds)
let myInput = document.getElementById('myInput');

//on keyup, start the countdown
myInput.addEventListener('keyup', () => {
    clearTimeout(typingTimer);
    if (myInput.value) {
        typingTimer = setTimeout(doneTyping, doneTypingInterval);
    }
});

//user is "finished typing," do something
function doneTyping () {
    //do something
}

This solution does use ES6 but it's not necessary here. Just replace let with var and the arrow function with a regular function.

NonameSL
  • 1,405
  • 14
  • 27
going
  • 9,669
  • 4
  • 36
  • 37
  • I believe the problem you are having is that you are typing fast enough that you push the second key before releasing the first. – Mala Feb 11 '12 at 00:57
  • 1
    This isn't a problem he is experiencing, it's a problem any user could duplicate as I've just done. Imagine if at the end of the timeout you postback ajax to the server and do something with a response. The accepted answer would have posted back repeatedy for me. Put an alert and type very fast. It will not only trigger at the end of the timeout, for me, it triggered repeatedly. This is the most complete answer, and it uses less code, kudos. – Dave Jellison Dec 19 '13 at 05:58
  • 7
    Well, it might be useful to still call the function even if the input is empty. What if you want to clear some search results (for example) when the input is blank again? – rvighne Feb 05 '14 at 03:01
  • 7
    I think we should also consider the condition where the user simply pastes come string inside the field . – Vishal Nair Aug 01 '14 at 09:49
  • 16
    use `$('#myInput').on('input', function() {` if you want this to work with copy and paste. I don't see the point of the check if input is empty. Supposing he wants to erase what he typed this will fail to make the ajax call. – But those new buttons though.. Oct 06 '14 at 05:07
  • 3
    Why it's `if ($('#myInput').val)` instead of `if ($('#myInput').val())`? Should `.val` be a function? – wlnirvana Dec 09 '14 at 06:34
  • 5
    Why not use $(this).val()? – JoelFan Feb 26 '15 at 23:20
  • 1
    @JoelFan or just `this.value`, it is so close that you don't need extra effort. – Castro Roy Apr 29 '16 at 20:27
  • 5
    for me it calls the function immediately :/ not after the 5 seconds, i've tried increasing the time, but the same thing happens – Xsmael May 08 '18 at 23:46
  • 1
    @Xsmael, I think passing an argument directly to the setTimeout like this: `setTimeout(doneTyping(argument), doneTypingInterval);` makes the function run twice. – vpibano Nov 26 '18 at 10:20
  • You can pass the event's argument to the `setTimeout` function using its 3rd parameter. [MDN](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout) `function doneTyping(evt) {...}` `myInput.addEventListener('keyup', evt => { clearTimeout(typingTimer); if (myInput.value) { typingTimer = setTimeout(doneTyping, doneTypingInterval, evt); } });` – Keith DC Sep 03 '20 at 03:40
  • it works perfect for me however is it normal that after the function is called the first time, it will not be called again? what is the user type again after 5 seconds? thanks – Manza Oct 08 '20 at 23:29
89

It's just one line with underscore.js debounce function:

$('#my-input-box').keyup(_.debounce(doSomething , 500));

This basically says doSomething 500 milliseconds after I stop typing.

For more info: http://underscorejs.org/#debounce

StudioTime
  • 22,603
  • 38
  • 120
  • 207
bit2pixel
  • 1,122
  • 8
  • 8
  • 7
    Seems to be available for jQuery, too: http://code.google.com/p/jquery-debounce/ and https://github.com/diaspora/jquery-debounce – koppor May 02 '13 at 11:37
  • 114
    It amazes me how people tout "ONE LINE OF CODE" as being critically important. Great. ONE LINE of code that requires a 1.1k JS file to load. I am not downvoting this because is a solution. But the one line bold thing irks me as well as leading to bloat code. – Wolfie May 20 '16 at 16:17
  • 9
    @Wolfie, I agree with you about code bloat but Underscore and Lodash are the top two depended-upon utilities at [NPM](https://www.npmjs.com/) so this may be a one-line solution for many people. – Paul May 28 '16 at 20:17
  • 3
    @Paul I agree that if you are loading the libraries for other purposes, its fine and actually beneficial to use common code. But to load over a K of code for the purpose of one line isn't efficient. Especially with mobile platforms becoming less and less unlimited data. And many Euro areas also pay for internet by the data. So its good to pay attention to bloat when reasonable to do so. – Wolfie Jun 03 '16 at 16:19
  • why i got : `Uncaught ReferenceError: _ is not defined` – Wilf Nov 15 '16 at 03:35
  • 1
    @Wolfie He does give the qualifier that it is one line WITH underscore. – wuliwong Oct 10 '17 at 19:40
  • 4
    You can just get the debouce() function, https://www.npmjs.com/package/debounce if that's all you need. Works pretty well. – darelf Mar 15 '18 at 15:28
75

Late answer but I'm adding it because it's 2019 and this is entirely achievable using pretty ES6, no third party libraries, and I find most of the highly rated answers are bulky and weighed down with too many variables.

Elegant solution taken from this excellent blog post.

function debounce(callback, wait) {
  let timeout;
  return (...args) => {
      clearTimeout(timeout);
      timeout = setTimeout(function () { callback.apply(this, args); }, wait);
  };
}

window.addEventListener('keyup', debounce( () => {
    // code you would like to run 1000ms after the keyup event has stopped firing
    // further keyup events reset the timer, as expected
}, 1000))
Beginner
  • 5,277
  • 6
  • 34
  • 71
GrayedFox
  • 2,350
  • 26
  • 44
  • 9
    This really should be the top answer given the year – Dark Hippo May 28 '20 at 06:52
  • 1
    Edited to further remove unneeded variables - can get around having to encapsulate `this` by passing in a normal instead of arrow function to `setTimeout()` – GrayedFox Jul 30 '20 at 13:46
  • how would I get the event inside the event handler function? – Beginner Jan 27 '21 at 13:08
  • Why does this not work outside of an event listener? e.g. if I run `debounce(() => {alert("test")}, 1000)`, and I do not run it again within 1000ms, shouldn't it run the callback and alert test? – Gradyn Wursten Sep 23 '21 at 22:46
  • This solution is not working. Also any other solution I found is not working @GrayedFox – Akhila May 14 '22 at 19:38
  • 2
    @Akhila I can’t help you if your description of the problem is “it’s not working”. Consider opening your own SO question and linking back to this thread. Peace. – GrayedFox May 16 '22 at 02:43
48

Yes, you can set a timeout of say 2 seconds on each and every key up event which will fire an ajax request. You can also store the XHR method and abort it on subsequent key press events so that you save bandwith even more. Here's something I've written for an autocomplete script of mine.

var timer;
var x;

$(".some-input").keyup(function () {
    if (x) { x.abort() } // If there is an existing XHR, abort it.
    clearTimeout(timer); // Clear the timer so we don't end up with dupes.
    timer = setTimeout(function() { // assign timer a new timeout 
        x = $.getJSON(...); // run ajax request and store in x variable (so we can cancel)
    }, 2000); // 2000ms delay, tweak for faster/slower
});

Hope this helps,

Marko

Marko
  • 71,361
  • 28
  • 124
  • 158
  • 1
    I would not recommend this. The code triggers an API request *each time* a key is pressed. Then it cancels the request if another key is pressed. Even though this solution prevents the AJAX callback to be triggered while the user types, it will still hammer the server with requests. – lxg Mar 27 '17 at 12:31
  • Ixg, no this won't. The clearTimeout function he uses clears the previous Timeout events, therefor invoking the API call. – Giga Chad Coding Mar 30 '18 at 18:00
  • @JohnSmith, i disagree it's probably better to first clear the timer, then check if xhr is empty or whatever your condition might be and finally run the timer with the ajax call – luukvhoudt May 11 '18 at 18:51
22
var timer;
var timeout = 1000;

$('#in').keyup(function(){
    clearTimeout(timer);
    if ($('#in').val) {
        timer = setTimeout(function(){
            //do stuff here e.g ajax call etc....
             var v = $("#in").val();
             $("#out").html(v);
        }, timeout);
    }
});

full example here: http://jsfiddle.net/ZYXp4/8/

azzy81
  • 2,261
  • 2
  • 26
  • 37
  • This worked for me, though keep in mind that when the user tabs-out of the text box you won't get a keyup event as it has lost focus by that point. Setting it to keydown instead seems to solve the problem. – horatius83 Sep 27 '13 at 19:51
14

Both top 2 answers doesn't work for me. So, here is my solution:

var timeout = null;

$('#myInput').keyup(function() {
    clearTimeout(timeout);

    timeout = setTimeout(function() {
        //do stuff here
    }, 500);
});
YSFKBDY
  • 735
  • 1
  • 12
  • 38
13

Modifying the accepted answer to handle additional cases such as paste:

//setup before functions
var typingTimer;                //timer identifier
var doneTypingInterval = 2000;  //time in ms, 2 second for example
var $input = $('#myInput');

// updated events 
$input.on('input propertychange paste', function () {
    clearTimeout(typingTimer);
    typingTimer = setTimeout(doneTyping, doneTypingInterval);      
});

//user is "finished typing," do something
function doneTyping () {
  //do something
}
Jecoms
  • 2,558
  • 4
  • 20
  • 31
13

Declare the following delay function:

var delay = (function () {
    var timer = 0;
    return function (callback, ms) {
        clearTimeout(timer);
        timer = setTimeout(callback, ms);
    };
})()

and then use it:

let $filter = $('#item-filter');
$filter.on('keydown', function () {
    delay(function () {            
        console.log('this will hit, once user has not typed for 1 second');            
    }, 1000);
});    
Fabian Bigler
  • 10,403
  • 6
  • 47
  • 70
11

I like Surreal Dream's answer but I found that my "doneTyping" function would fire for every keypress, i.e. if you type "Hello" really quickly; instead of firing just once when you stop typing, the function would fire 5 times.

The problem was that the javascript setTimeout function doesn't appear to overwrite or kill the any old timeouts that have been set, but if you do it yourself it works! So I just added a clearTimeout call just before the setTimeout if the typingTimer is set. See below:

//setup before functions
var typingTimer;                //timer identifier
var doneTypingInterval = 5000;  //time in ms, 5 second for example

//on keyup, start the countdown
$('#myInput').on("keyup", function(){
    if (typingTimer) clearTimeout(typingTimer);                 // Clear if already set     
    typingTimer = setTimeout(doneTyping, doneTypingInterval);
});

//on keydown, clear the countdown 
$('#myInput').on("keydown", function(){
    clearTimeout(typingTimer);
});

//user is "finished typing," do something
function doneTyping () {
    //do something
}

N.B. I would have liked to have just added this as a comment to Surreal Dream's answer but I'm a new user and don't have enough reputation. Sorry!

Liam Flanagan
  • 408
  • 5
  • 11
7

const inText = document.getElementById('inText')
const outText = document.getElementById('outText')
const delay = 1000

let timer
inText.addEventListener('input', code => {
  clearTimeout(timer);
  timer = setTimeout(x => {
    outText.innerHTML = inText.value
  }, delay, code)
})
<textarea id='inText'>edit this and...</textarea>
<pre id='outText'>see the results after you stop typing for one second</pre>
David Contreras
  • 169
  • 1
  • 3
6

I don't think keyDown event is necessary in this case (please tell me why if I'm wrong). In my (non-jquery) script similar solution looks like that:

var _timer, _timeOut = 2000; 



function _onKeyUp(e) {
    clearTimeout(_timer);
    if (e.keyCode == 13) {      // close on ENTER key
        _onCloseClick();
    } else {                    // send xhr requests
        _timer = window.setTimeout(function() {
            _onInputChange();
        }, _timeOut)
    }

}

It's my first reply on Stack Overflow, so I hope this helps someone, someday:)

Rafal Pastuszak
  • 3,100
  • 2
  • 29
  • 31
4

I was implementing the search at my listing and needed it to be ajax based. That means that on every key change, searched results should be updated and displayed. This results in so many ajax calls sent to server, which is not a good thing.

After some working, I made an approach to ping the server when the user stops typing.

This solution worked for me:

$(document).ready(function() {
    $('#yourtextfield').keyup(function() {
        s = $('#yourtextfield').val();
        setTimeout(function() { 
            if($('#yourtextfield').val() == s){ // Check the value searched is the latest one or not. This will help in making the ajax call work when client stops writing.
                $.ajax({
                    type: "POST",
                    url: "yoururl",
                    data: 'search=' + s,
                    cache: false,
                    beforeSend: function() {
                       // loading image
                    },
                    success: function(data) {
                        // Your response will come here
                    }
                })
            }
        }, 1000); // 1 sec delay to check.
    }); // End of  keyup function
}); // End of document.ready

You will notice that there is no need to use any timer while implementing this.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Ata ul Mustafa
  • 1,172
  • 12
  • 18
4

Well, strictly speaking no, as the computer cannot guess when the user has finished typing. You could of course fire a timer on key up, and reset it on every subsequent key up. If the timer expires, the user hasn't typed for the timer duration - you could call that "finished typing".

If you expect users to make pauses while typing, there's no way to know when they are done.

(Unless of course you can tell from the data when they are done)

sleske
  • 81,358
  • 34
  • 189
  • 227
4

agree with the @going 's answer. Another similar solution that worked for me is the one below. The only difference is that I am using .on("input"...) instead of keyup. This only captures changes in the input. other keys like Ctrl, Shift etc. are ignored

var typingTimer;                //timer identifier
var doneTypingInterval = 5000;  //time in ms (5 seconds)

//on input change, start the countdown

$('#myInput').on("input", function() {    
    clearTimeout(typingTimer);
    typingTimer = setTimeout(function(){
        // doSomething...
    }, doneTypingInterval);
});
Grace.io
  • 71
  • 3
3

I feel like the solution is somewhat a bit simpler with the input event:

var typingTimer;
var doneTypingInterval = 500;

$("#myInput").on("input", function () {
    window.clearTimeout(typingTimer);
    typingTimer = window.setTimeout(doneTyping, doneTypingInterval);
});

function doneTyping () {
    // code here
}
Adrien Brunelat
  • 4,492
  • 4
  • 29
  • 42
3

I just figured out a simple code to wait for user to finish typing:

step 1.set time out to null then clear the current timeout when the user is typing.

step 2.trigger clear timeout to the variable define before keyup event is triggered.

step 3.define timeout to the variable declared above;

<input type="text" id="input" placeholder="please type" style="padding-left:20px;"/>
<div class="data"></div>

javascript code

var textInput = document.getElementById('input');
var textdata = document.querySelector('.data');
// Init a timeout variable to be used below
var timefired = null;

// Listen for keystroke events
// Init a timeout variable to be used below
var timefired = null;// Listen for keystroke events
textInput.onkeyup = function (event) {
clearTimeout(timefired);
timefired = setTimeout(function () {
    textdata.innerHTML = 'Input Value:'+ textInput.value;
  }, 600);
};
2

This is the a simple JS code I wrote:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="pt-br" lang="pt-br">
<head><title>Submit after typing finished</title>
<script language="javascript" type="text/javascript">
function DelayedSubmission() {
    var date = new Date();
    initial_time = date.getTime();
    if (typeof setInverval_Variable == 'undefined') {
            setInverval_Variable = setInterval(DelayedSubmission_Check, 50);
    } 
}
function DelayedSubmission_Check() {
    var date = new Date();
    check_time = date.getTime();
    var limit_ms=check_time-initial_time;
    if (limit_ms > 800) { //Change value in milliseconds
        alert("insert your function"); //Insert your function
        clearInterval(setInverval_Variable);
        delete setInverval_Variable;
    }
}

</script>
</head>
<body>

<input type="search" onkeyup="DelayedSubmission()" id="field_id" style="WIDTH: 100px; HEIGHT: 25px;" />

</body>
</html>
2

Why not just use onfocusout?

https://www.w3schools.com/jsreF/event_onfocusout.asp

If it's a form, they will always leave focus of every input field in order to click the submit button so you know no input will miss out on getting its onfocusout event handler called.

coffeeeee
  • 129
  • 1
  • 12
  • 2
    onfocusout had poor support when the question was asked (nearly 7 years ago now), and also the effect with onfocusout would be somewhat different. You would have to wait for the user to leave focus on the element whereas with the timeout/debounce solution it "fires" as soon as the user stops typing and the user is not required to switch focus. A use case example would be one of those registration forms where when the user stops entering a potential username a checkmark or "X" appears to the right of the form field indicating that the name is available. – David Zorychta Aug 29 '17 at 02:21
  • if the search bar is on focus, no one needs to come outside, after some time result will be directly shown as no open is typing any more – Satish Patro Dec 07 '19 at 06:32
2

Multiple timers per page

All the other answers only work for one control (my other answer included). If you have multiple controls per page (e.g. in a shopping cart) only the last control where the user typed something will get called. In my case this is certainly not the wished behaviour - each control should have its own timer.

To solve this, you simply have to pass an ID to the function and maintain a timeoutHandles dictionary as in the following code:

Function Declaration:

var delayUserInput = (function () {
    var timeoutHandles = {};    
    return function (id, callback, ms) {        
        if (timeoutHandles[id]) {
            clearTimeout(timeoutHandles[id]);
        }

        timeoutHandles[id] = setTimeout(callback, ms);             
    };
})();

Function Usage:

  delayUserInput('yourID', function () {
     //do some stuff
  }, 1000);
Fabian Bigler
  • 10,403
  • 6
  • 47
  • 70
2

Here is a solution that fires after 1 second of not typing, but also fires instantly when the input is blank. This is useful when clearing search results after the user deletes the input query. This solution also supports copying and pasting into the search box. The $(() => { ... }); wrapping the top portion of code simply means "do this when the page is loaded" in simple Jquery terms.

var searchTimer;
var searchInterval = 1000;

$(() => {
    $('#search-box').on('input', (event) => {
        clearTimeout(searchTimer);
        searchTimer = setTimeout(() => {
            searchContacts(event.target.value);
        }, (event.target.value.length > 0) ? searchInterval : 0);
    });
});

function searchContacts(val) {
    console.log('searching: ' + val);
}
quemeful
  • 9,542
  • 4
  • 60
  • 69
1

You can use the onblur event to detect when the textbox loses focus: https://developer.mozilla.org/en/DOM/element.onblur

That's not the same as "stops typing", if you care about the case where the user types a bunch of stuff and then sits there with the textbox still focused.

For that I would suggest tying a setTimeout to the onclick event, and assuming that after x amount of time with no keystrokes, the user has stopped typing.

Mike Ruhlin
  • 3,546
  • 2
  • 21
  • 31
1

If there is necessity for the user to move away from the field, we can use "onBlur" instead of Onchange in Javascript

  <TextField id="outlined-basic"  variant="outlined" defaultValue={CardValue} onBlur={cardTitleFn} />

If that is not necessary setting timer would be the good option.

1

for alpine.js users <input @input.debounce.500ms="fn()">

Paul
  • 461
  • 4
  • 7
0

If you are looking for a specific length (such as a zipcode field):

$("input").live("keyup", function( event ){
if(this.value.length == this.getAttribute('maxlength')) {
        //make ajax request here after.
    }
  });
tkorkunckaya
  • 164
  • 3
  • 15
Jason
  • 7,612
  • 14
  • 77
  • 127
0

Not sure if my needs are just kind of weird, but I needed something similar to this and this is what I ended up using:

$('input.update').bind('sync', function() {
    clearTimeout($(this).data('timer'));            
    $.post($(this).attr('data-url'), {value: $(this).val()}, function(x) {
        if(x.success != true) {
            triggerError(x.message);    
        }
    }, 'json');
}).keyup(function() {
    clearTimeout($(this).data('timer'));
    var val = $.trim($(this).val());
    if(val) {
        var $this = $(this);
        var timer = setTimeout(function() {
            $this.trigger('sync');
        }, 2000);
        $(this).data('timer', timer);
    }
}).blur(function() {
    clearTimeout($(this).data('timer'));     
    $(this).trigger('sync');
});

Which allows me to have elements like this in my application:

<input type="text" data-url="/controller/action/" class="update">

Which get updated when the user is "done typing" (no action for 2 seconds) or goes to another field (blurs out of the element)

Paolo Bergantino
  • 480,997
  • 81
  • 517
  • 436
0

If you need wait until user is finished with typing use simple this:

$(document).on('change','#PageSize', function () {
    //Do something after new value in #PageSize       
});

Complete Example with ajax call - this working for my pager - count of item per list:

$(document).ready(function () {
    $(document).on('change','#PageSize', function (e) {
        e.preventDefault();
        var page = 1;
        var pagesize = $("#PageSize").val();
        var q = $("#q").val();
        $.ajax({
            url: '@Url.Action("IndexAjax", "Materials", new { Area = "TenantManage" })',
            data: { q: q, pagesize: pagesize, page: page },
            type: 'post',
            datatype: "json",
            success: function (data) {
                $('#tablecontainer').html(data);
               // toastr.success('Pager has been changed', "Success!");
            },
            error: function (jqXHR, exception) {
                ShowErrorMessage(jqXHR, exception);
            }
        });  
    });
});    
Tunaki
  • 132,869
  • 46
  • 340
  • 423
Miroslav Siska
  • 401
  • 4
  • 15
0

Once you detect focus on the text box, on key up do a timeout check, and reset it each time it's triggered.

When the timeout completes, do your ajax request.

ocodo
  • 29,401
  • 18
  • 105
  • 117
0

Simple and easy to understand.

var mySearchTimeout;
$('#ctl00_mainContent_CaseSearch').keyup(function () {
   clearTimeout(mySearchTimeout);
   var filter = $(this).val();
   mySearchTimeout = setTimeout(function () { myAjaxCall(filter); }, 700);
   return true;
});
Juls
  • 658
  • 6
  • 15
0

For passing parameters to your function along with ES6 syntax.

$(document).ready(() => {
    let timer = null;
     $('.classSelector').keydown(() => {
     clearTimeout(timer); 
     timer = setTimeout(() => foo('params'), 500);
  });
});

const foo = (params) => {
  console.log(`In foo ${params}`);
}
ajinkya
  • 31
  • 2
0

Not a direct answer bu if someone looking for an AngularJS solution. I wrote a directive according to the popular solutions here.

 app.directive("ngTypeEnds", ["$timeout", function ($timeout) {
    return function (scope, element, attrs) {
        var typingTimer;               
        element.bind("keyup", function (event) {
            if (typingTimer)
                $timeout.cancel(typingTimer);
            if (angular.element(element)[0].value) {
                typingTimer = $timeout(function () {
                    scope.$apply(function () {
                        scope.$eval(attrs.ngTypeEnds);
                    });
                }, 500);
            }
            event.preventDefault();
        });
    };
}]);
Görkem Öğüt
  • 1,761
  • 3
  • 18
  • 29
0

You guys have heard of closures in javascript ?!

it's very simple and straightforward just compare you current input value with the old value that the setTimeOut function closes over, and voila, you're done.

let timer;
$('#myInput').on('keyup', function() {
  window.clearTimeout(timer);
  // here is the closures javascript magic happens.
  const value = $(this).val();
  timer = setTimeout(() => {
    if(value === $(this).val() && $(this).val()!== ''){
        alert($(this).val());
    }
  }, 500);
})
0

I needed mine to run for a specific control and this worked for me :

function debounce(func, timeout) {
            let timer;
            return (...args) => {
                clearTimeout(timer);
                timer = setTimeout(() => { func.apply(this, args); }, timeout);
            };
        }

$('#txtFilterClientCode').keyup(debounce(function () {
            console.log("Test");
        }, 1000));
-2

Wow, even 3 comments are pretty correct!

  1. Empty input is not a reason to skip function call, e.g. I remove waste parameter from url before redirect

  2. .on ('input', function() { ... }); should be used to trigger keyup, paste and change events

  3. definitely .val() or .value must be used

  4. You can use $(this) inside event function instead of #id to work with multiple inputs

  5. (my decision) I use anonymous function instead of doneTyping in setTimeout to easily access $(this) from n.4, but you need to save it first like var $currentInput = $(this);

EDIT I see that some people don't understand directions without the possibility to copy-paste ready code. Here you're

var typingTimer;
//                  2
$("#myinput").on('input', function () {
    //             4     3
    var input = $(this).val();
    clearTimeout(typingTimer);
    //                           5
    typingTimer = setTimeout(function() {
        // do something with input
        alert(input);
    }, 5000);      
});
vladkras
  • 16,483
  • 4
  • 45
  • 55
  • How this answers the question? – Thomas Orlita Jun 13 '17 at 07:12
  • @ThomasOrlita this answer supplements 3 most rated answers. Take a look at accepted one: 1. it doesn't support `paste`, it doesn't use `this`, etc. (I think it's important but I don't want to get lost in tons of comments) – vladkras Jun 13 '17 at 16:37
  • 1
    This does not answer the question. If you would like to comment about another answer, there is a commenting system (I'm using it right now!) – GrayedFox Sep 02 '19 at 21:50