Home > Ajax, JavaScript, Programming, Web > Onkeyup delay in JavaScript

Onkeyup delay in JavaScript

In a recent post I used a little script to delay the onkeyup event for a set amount of milliseconds after the key was released.  That way a callback will not be made for every key that gets released, but only once the user stopped typing.  That post was brought up many times by a search for this specific function, so I decided to extract it into its own post.

For this post I will only show an alert message to keep it to the bare minimum.

First we need to add this to our page, preferably in the <head> section.  Have a look here if you are not familiar with XHTML compliant JavaScript, it’s a short and straight to the point post.

<script language="javascript" type="text/javascript">
    //<![CDATA[
    function delayTimer() {
        var timer;
        return function(fun, time) {
            clearTimeout(timer);
            timer = setTimeout(fun, time);
        };
    }
    var delayFunction = delayTimer();

    function showMessage() {
        alert('Delay expired!');
    }
    //]]>
</script>

This is just a timer which restarts itself every time the onkeyup event triggers.  Once it runs out it will execute the function that you specify later.

<input id="txtDelay" type="text" onkeyup="delayFunction(showMessage, 500);" />

This is our input box which uses the onkeyup delay.  We use our delegate that we created and pass in two parameters, the first is the function to call and the second is the delay before doing so.  In this case it will wait 500ms before calling the showMessage function.

That is all you need to get a delay for an event in JavaScript.

NOTE: This does NOT work when pasting text in using the mouse.  Disable right-click on the input box if you rely on this function to execute.

Hope this is clear.  Comment if there is anything unclear or if you have a better way for me to do things.

Update – Passing in a parameter to the function as requested by a comment

<script language="javascript" type="text/javascript">
    //<![CDATA[
    function delayTimer() {
        var timer;
        return function (fun, time) {
            clearTimeout(timer);
            timer = setTimeout(fun, time);
        };
    }

    var delayFunction = delayTimer();

    // Takes parameters to display
    function showMessage(message, sender) {
        alert(message + " - " + $(sender).val());
    }
    //]]>
</script>
<!-- Now passing in a function with parameters -->
<input id="txtDelay" type="text" onkeyup="delayFunction(function() { showMessage('Display this string!', $('#txtDelay')); }, 500);" />

Note that passing in ‘this’ to the function in the onclick event does not send the input through.

Advertisements
  1. default_coder
    17 June 2011 at 15:00

    Hi, great example. Finally one that works…just wondering how would you change this code if your method (showmessage) had parameters..thanks

  2. Pieter
    17 June 2011 at 20:50

    Hi default_coder,

    Glad this actually helped someone 🙂

    I have updated the post to include some code on sending parameters to the method, hope this is what you were looking for. Note that I use jQuery selector which you should replace if you are not using jQuery.

  3. 12 October 2011 at 11:16

    Thanks nice work

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: