Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with java script and time delay

Status
Not open for further replies.

meenu24

Programmer
May 5, 2006
37
US
Hi, I have a logic, where I have to trigger the java script after a time delay but with little complications.
In a text field I am typing the word for search. If I type a letter and there is no further keyboard acitvity for one second it should call the java script automatically. In the same time, if I type a letter wait for half a second and again type a letter it should call the java script after I typed the second letter and and there is no keyboard activity for one second. This can be achieved by onkeyup and timedelay. But I am confused about how do this through java script. I am also wondering is there any way where I can identify the keyboard inactivity by any means.

Thanks for any kind of suggestion.

Thanks
 
Use your onkeydown event to call a function that checks to see if a timer was set and to cancel it, use the onkeyup event to set the timer.
This way if the timer reaches maximum it runs the javascript you want but if a key is pressed DOWN before the timer has completed the timer get's canceled. Once the key is released again it sets the timer anew.

I would give more than 1 second between keystrokes though.


It's hard to think outside the box when I'm trapped in a cubicle.
 
Thanks theniteowl for your idea,Timer should be set by the event like onkeyup. Based on your suggesiton I am planning a logic as given below.
Thats is on firstkeyup the timer starts and if it reahces 1000ms then trigger a function. When the second keyup happends before one second again the timer is reset to begin from beginning. But as I am new to java script I was just wondering how to do this using java script. Any suggestion / scripts.

THanks in advance
 
I threw together sample code for what I was saying.
The way it works is anytime a key is pressed while focus is on that field it will cancel any existing timer and as soon as the key is released it will create a new timer.

If the user starts to type and stops for any reason though the code is going to execute and may not be a good method in the long run.

Code:
<html>
<head>
<title>Testing</title>
</head>
<body>
<script type="text/javascript">
var timer;
function startTimer() {
  stopTimer();
  timer = setTimeout('doSomething()', 5000);
}

function stopTimer() {
  if (window.timer) clearTimeout(timer);
}

function doSomething(){
  alert("Did something");
}
</script>
<body>
<input type="text" onkeydown="stopTimer();" onkeyup="startTimer();">
</body>
</html>


It's hard to think outside the box when I'm trapped in a cubicle.
 
Thanks you very much for the code, it worked, but there is always a error on page for first time in the stauts window. But it is working properly. Can anyone tell why this error in page but still the program works properly.

Thanks again
 
Remove window. from if (window.timer) and see if that helps.


It's hard to think outside the box when I'm trapped in a cubicle.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top