Expand|Select|Wrap|Line Numbers
- function reviewNick() {
- if (document.getElementById('nick').value.length < 6) {
- document.getElementById('nickavail').innerHTML = '<div class="nna">Your nickname must be six characters or longer.</div>';
- }
- else {
- if (nickTimeout) { window.clearTimeout(nickTimeout); }
- document.getElementById('nickavail').innerHTML = '<div class="nna"><em>Checking nickname for availability...</em></div>'
- var nickTimeout = window.setTimeout('checkNick();', 5000);
- }
- }
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?