Problems with setTimeout() [Javascript]

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
dhrosti
Forum Commoner
Posts: 90
Joined: Wed Jan 10, 2007 5:01 am
Location: Leeds, UK

Problems with setTimeout() [Javascript]

Post by dhrosti »

Im trying to set a timer on my ajax request so the server isn't overloaded, but having trouble actually getting the timer to work...

i use, onkeyup="timer(this)" on a text input field, but no request is sent/received from the server.

Code: Select all

function timer(obj) {
	
	window.setTimeout("sendReq("+obj.value+", "+obj.id+", "+obj.id+"_valid)", 500);
	
}
The sendReq() function..

Code: Select all

function sendReq(str, objId, responseId)
Any help would be much appreciated.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

If what you're trying to do it wait until the user is done typing before an ajax validation, you'll need to cancel (or overwrite) the timer on every keystroke.

Code: Select all

function timer(obj) {
        window.ajaxValidationTimer = window.setTimeout("sendReq('"+obj.value+"','"+obj.id+"','"+obj.id+"_valid')", 500);
}
That way if another keyup event fires before the 500ms is up, the existing timer will be overwritten with a fresh 500ms timeout.

Also - you'll need quotes around those strings (I added them for you).
User avatar
dhrosti
Forum Commoner
Posts: 90
Joined: Wed Jan 10, 2007 5:01 am
Location: Leeds, UK

Post by dhrosti »

good stuff, thanks.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Javascript is a client side language.

Moving to Client-side.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply