28

Hi Again Masters Of The Web :) Now, I have got a new stupid question, and I am asking to forgive me. I read everywhere about this solution, but didn't find the one that works for me.

I have got:

<input name="domain" type="text" id="domain" onKeyUp="javascript:chk_me();">

All I am asking is how to make this not to check after a button is pressed, but after to say 1000 miliseconds of keyboard inactivity?

Spoonk
  • 705
  • 2
  • 10
  • 17

4 Answers4

41

Try this:

var timer;
function chk_me(){
   clearTimeout(timer);
   timer=setTimeout(function validate(){...},1000);
}

In this way every time a key is pressed, the timeout will be deleted and the set again.

Javasick
  • 2,753
  • 1
  • 23
  • 35
mck89
  • 18,918
  • 16
  • 89
  • 106
  • 2
    I think `var timer` needs to be outside the function body – Rodrigo Sep 04 '09 at 22:17
  • you are right the timer must be set outside the function, i've edit the answer – mck89 Sep 04 '09 at 22:18
  • Thanks! Best answer of the lot by a fair margin. – Shog9 Sep 04 '09 at 22:22
  • do you think that use validate.apply(this) will work? i don't know how to preserve the this context – mck89 Sep 04 '09 at 22:22
  • Hi all. No, this does not work again :( I'm sorry for my stupid questions, but... where I must put this? Inside tags, or in ? Whatever, I tried both methods, and It doesn't works :( See by yourself: http://spoonk.eu/includes/home/domain/domain.php – Spoonk Sep 04 '09 at 22:23
  • After reading his comment to Berzemus, i'm not even sure it'd matter... He might not even be using it. But FWIW, you'd just save it in a local variable in `chk_me()`, let the `validate()` closure capture it automatically, and then use that variable in lieu of `this` within `validate()`. – Shog9 Sep 04 '09 at 22:24
  • Move the script into the head and i will check the example again – mck89 Sep 04 '09 at 22:26
  • @Spoonk: either edit the definition of `chk_me()` in `domain.js` to include mck89's logic, or name mk89's chk_me function something else (`delay_chk()`...) and then call the original `chk_me()` in place of `validate()`. – Shog9 Sep 04 '09 at 22:27
  • Just A sec, to do what you all said :) – Spoonk Sep 04 '09 at 22:29
  • now i see that you don't have replaced the ... in the function. Inside the validate funcion you must validate the fild or call another validation function – mck89 Sep 04 '09 at 22:29
  • Jesus, I'm obviously in deep shi*s... After all this time I spend learning how_to do stuffs, It becomes clear that I'm just one good... copy/paster... I can't understand what and where I need to replace... – Spoonk Sep 04 '09 at 22:33
  • the validate(){...} was only an example. Delete those dots and replace them with the validation code. First rename my function into something like chk_me2 (because a chk_me function already exists) and then call the orignal function into the validate function: validate(){chk_me()} – mck89 Sep 04 '09 at 22:37
  • change also the code in the onKeyUp attribute: javascript:delay_chk(); – mck89 Sep 04 '09 at 22:41
  • mck89, I did what you told me right now, but... I think I'm so fuc*n dumb or some kind of retarded... It's not working. Can you check it again please? – Spoonk Sep 04 '09 at 22:41
  • YES!!! DAMNED I'T WORKS!!!!!!! THANKS mck89!!! I don't know what else to say to you!!! – Spoonk Sep 04 '09 at 22:43
  • and next time don't copy/paste :) – mck89 Sep 04 '09 at 22:45
  • Next time... Right after this night I'm going to buy all books about JavaScript that we have in our dummy country and will learn everything! :))) Thanks a lot mck89, wish I have your knowledge... – Spoonk Sep 04 '09 at 22:55
16

Another approach, without globals:

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

Usage:

Attaching the event through JavaScript:

window.onload = function () {
    document.getElementById('domain').onkeyup = function() {
        typewatch(function(){alert('Time elapsed!');}, 1000 );
    };
};

Or using an inline event handler (not so much recommended) as you have in your example:

<input type="text" name="domain" id="domain"
   onKeyUp="typewatch(function(){alert('Time elapsed!');}, 1000 );"/>

Try a demo here.

Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
1

Really useful post!

Another approach without globals, using function properties:

function chk_me(){
   if(typeof timer !== undefined){
     clearTimeout(this.timer);
   }
   this.timer=setTimeout(function validate(){validate(...)},1000);
}
PatrikJ
  • 2,327
  • 3
  • 24
  • 35
-3

setTimeOut() would be the one ;)

<input name="domain" type="text" id="domain" onKeyUp="setTimeout('chk_me()',1000);">

link text

Berzemus
  • 3,618
  • 23
  • 32
  • This will not preserve the value of the `this` context variable when calling `chk_me()`. Also, using the variation of `setTimeout()` that takes and evaluates a string containing code is both pointless in this scenario and messy in all scenarios. – Shog9 Sep 04 '09 at 22:17
  • Hi Berzemus, thanks for your reply. I've all ready tried this. It works, but the problem is that it does not delays the script execution itself. It delays after every single key press... – Spoonk Sep 04 '09 at 22:17