I'm trying to make a little script to check the availability of
usernames. I'm doing it with the following javascript (currently
outputting information for debugging)...

$("input[name='username']").keyup(checkIfUsernameExists);

function checkIfUsernameExists() {
        username = $("input[name='username']")[0].value;
        $('#registrationUsernameAlert').append('Sending for ' + username +
"...<br />");
        ajaxUrl = "<? echo $AJAX_CHECK_IF_USERNAME_EXISTS_URL; ?>";
        $.getJSON(ajaxUrl, {
                username: username
        }, usernameAvailabilityReceived);
}

function usernameAvailabilityReceived(json) {
        $('#registrationUsernameAlert').append('Received ' + json.username +
"... Available? " + json.available + ".<br />");
        if (json.available == "false") {
                if ($("input[name='username']").val() == json.username) {
                        /* $('#registrationUsernameAlert').text("<== That user 
name isn't
available."); */
                }
        }
}

If I type slowly, waiting for the response from the last call to be
received, then everything works fine. But if I type quickly, it seems
to get held up on the results from past requests. Below is an example
of the output I might receive if I type slowly for the first two or
three characters, then quickly for several, and then slowly again at
the end. What might I do to correct this problem? Is there any way I
can abort past requests when a new one comes along???

Sending for n...
Received n... Available? true.
Sending for ne...
Received ne... Available? true.
Sending for nes...
Received nes... Available? true.
Sending for nest...
Received nes... Available? true.
Sending for nestor...
Received nes... Available? true.
Sending for nestor...
Received nes... Available? true.
Sending for nestora...
Received nes... Available? true.
Sending for nestoral...
Received nes... Available? true.
Sending for nestoralva...
Received nes... Available? true.
Sending for nestoralva...
Received nes... Available? true.
Sending for nestoralvar...
Received nes... Available? true.
Sending for nestoralvare...
Received nes... Available? true.
Sending for nestoralvarez...
Received nestoralvarez... Available? false.

Reply via email to