I am very new to jQuery so need a bit of advice. I would like to call an ajax function to search a db for usernames to see if the entered value is taken. If the entered value is then the php page returns ‘invalid’ and if it isnt it returns ‘valid’.
The problem I ahev having is that I cannot parse data from the ajax result without setting it to synchronous. I do not want to do this because it freezes the browser if you type something in. This is an example of what my code looks like. What can I do to get rid of the ‘async: false’? My first idea was use a timeout so that it does not process on each key down, it only processes if there is more than one second between each key entered. But this is not a great solution.
[code=html]function validateElement(el, v){
clearTimeout(timeout);
var returnValue;
timeout = setTimeout(function(){ $.ajax({
url: “scripts/validate.php”,
type: “GET”,
data: “el=”+el+”&v=”+v,
dataType: “html”,
cache: false,
ifModified: true,
async: false, // I want to get rid of this!!!!
success: function(data){
if(data==’valid’){returnValue=true;}
else if(data==’invalid’){returnValue=false;}
else{alert(‘error’)}
}
})}, 1000);
return returnValue;
}