Page 1 of 1

Problems with setTimeout() [Javascript]

Posted: Thu May 10, 2007 4:42 am
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.

Posted: Thu May 10, 2007 5:19 am
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).

Posted: Thu May 10, 2007 7:24 am
by dhrosti
good stuff, thanks.

Posted: Thu May 10, 2007 10:02 am
by pickle
Javascript is a client side language.

Moving to Client-side.