473,387 Members | 1,453 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Problems with clearTimeout

I haven't had occasion to use clearTimeout before, and this first go-round isn't going too well. Is there something obvious I'm doing wrong? Here, check this out:

Expand|Select|Wrap|Line Numbers
  1. function reviewNick() {
  2.     if (document.getElementById('nick').value.length < 6) {
  3.         document.getElementById('nickavail').innerHTML = '<div class="nna">Your nickname must be six characters or longer.</div>';
  4.     }
  5.     else {
  6.         if (nickTimeout) { window.clearTimeout(nickTimeout); }
  7.         document.getElementById('nickavail').innerHTML = '<div class="nna"><em>Checking nickname for availability...</em></div>'
  8.         var nickTimeout = window.setTimeout('checkNick();', 5000);
  9.     }
  10. }
  11.  
Here's what I'm trying to do here. This is for a registration page, and I'm checking nicknames against a database to see if they exist when a user enters one. If it does, I'm displaying a message saying so. This reviewNick function is getting called every time the user presses a key, and it, in turn, calls a function called checkNick for some AJAX action. But here's the tricky part: I don't want the message to refresh every time the user presses a key. Instead, I want to wait five seconds after the user is finished typing and then report whether the nickname is taken or not.

I'm using setTimeout to do so, but here's the problem with that. The user presses a key and the timer begins, right? But if the user keeps pressing keys, that same timer will still be going. Every time I get a key press, I want to cancel the previous setTimeout and start a new one, so the message only appears if the user hasn't typed anything for five seconds. I figured clearTimeout was the answer to this, as you can see above. But it's not working. The timer continues to go off five seconds after the first keystroke, not the last one. The browser doesn't seem to recognize the nickTimeout variable I'm setting -- I've tried testing with alerts, but the alerts say the variable is undefined. If I stick the alert after the variable definition, I get something. But if I put the alert at the beginning of the else statement, it's undefined, even after a whole lot of keystrokes should have defined it a whole lot of times.

Any ideas what's going on here?
May 31 '07 #1
2 2871
pbmods
5,821 Expert 4TB
Expand|Select|Wrap|Line Numbers
  1. var nickTimeout = window.setTimeout('checkNick();', 5000);
By using the var keyword, you are scoping nickTimeout to your function rather than window.

Instead, you want to do this:

Expand|Select|Wrap|Line Numbers
  1. nickTimeout = window.setTimeout('checkNick();', 5000);
  2.  
  3. // Or...
  4. window.nickTimeout = window.setTimeout('checkNick();', 5000);
  5.  
  6. // Or...
  7. window['nickTimeout'] = window.setTimeout('checkNick();', 5000);
  8.  
May 31 '07 #2
Thanks a lot! Embarrassingly, I did not know that about the var keyword -- I always thought it was just there for stylistic reasons. The window.nickTimeout solution worked great, though I also had to change all other nickTimeout references to window.nickTimeout.
May 31 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Aaron | last post by:
I'm having problems getting my popup menu to work correctly, and was hoping someone here could point me in the right direction. I have the following script: <script> var activeMenu=""; ...
2
by: Adrian MacNair | last post by:
Hi, it's been about 5 years since I did JavaScript so I was wondering if I could get some help with a little problem. I wrote a script from memory (5 years!!) and the only problem I have is...
5
by: Corky | last post by:
This works: db2 SELECT DISTINCT PROBLEM_OBJECTS.PROBLEM_ID FROM PROBLEM_OBJECTS INNER JOIN PROBLEMS ON PROBLEM_OBJECTS.PROBLEM_ID = PROBLEMS.PROBLEM_ID WHERE INTEGER(DAYS(CURRENT DATE) -...
10
by: BBFrost | last post by:
We just recently moved one of our major c# apps from VS Net 2002 to VS Net 2003. At first things were looking ok, now problems are starting to appear. So far ... (1) ...
1
by: Bob | last post by:
Hi, Hope you can help me with this one. I'm at my wits end. I'm trying to create an intelligent edit-box like the excellent "Customer" one at the URL: ...
4
by: Seansan | last post by:
Hi, am having some troubles with timeouts. The code starts at verifyform. I was debugging and tested hitting the form submit twice (within 3 seconds)! In IE6 I get an error saying that I have an...
2
by: joey.powell | last post by:
Hey guys I have some js code as follows... ------- var SessionTimer; function StartSessionTimer() { SessionTimer = setTimeout('RedirectToSessionTimedOutPage(),60000) }
1
by: bizt | last post by:
Hi, I have a process where every so often I refresh a table's data using a setTimeout() .. and when its no longer needed, a clearTimeout(). The following is a simple example of how this is...
1
by: ehud37new | last post by:
this script work fine in IE but not in FireFox where is the problem? here is the script /*------------------------------------------------------------------ File: menu.js ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.