views:

2322

answers:

4

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?

A: 

setTimeOut() would be the one ;)

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

link text

Berzemus
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
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
+9  A: 

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.

mck89
I think `var timer` needs to be outside the function body
Rodrigo
you are right the timer must be set outside the function, i've edit the answer
mck89
Thanks! Best answer of the lot by a fair margin.
Shog9
do you think that use validate.apply(this) will work? i don't know how to preserve the this context
mck89
Hi all. No, this does not work again :( I'm sorry for my stupid questions, but... where I must put this? Inside <head> tags, or in <body>? Whatever, I tried both methods, and It doesn't works :(See by yourself: http://spoonk.eu/includes/home/domain/domain.php
Spoonk
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
Move the script into the head and i will check the example again
mck89
@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
Just A sec, to do what you all said :)
Spoonk
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
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
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
change also the code in the onKeyUp attribute: javascript:delay_chk();
mck89
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
YES!!! DAMNED I'T WORKS!!!!!!! THANKS mck89!!! I don't know what else to say to you!!!
Spoonk
but what's wrong now? it seems to work for for me.
mck89
and next time don't copy/paste :)
mck89
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
A: 

You can try the settimeout method:

javascript:setTimeout("chk_me()", 1000)
Noah
+4  A: 

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.

CMS
What makes you think that typewatch is not global?
Josh Stodola
yes typewatch is global, but I mean that the **timer** variable is not in the global scope, and can be only accessed by the typewatch function.
CMS
+1 Understood.
Josh Stodola