views:

40

answers:

1

Hello. Here is my code :

function search_buddy() 
{
    $.post("num.php",function (ret){
        num=ret;
    });
    $("#Layer7").html(num);
}
</script>

<div id="Layer8">
     Find a buddy : 
    <input type="text" id="search" onkeyup="search_buddy()"/>&nbsp;:
</div>

Now,when i enter one character into the text box with id=search, the function search_buddy doesn't seem to get triggered. Whereas, if i enter two characters or more,the function works perfectly.Why is this happening ?

A: 

Best resource on DOM events: http://www.quirksmode.org/dom/events/keys.html

It looks like your event handler "search_buddy" fires an AJAX request, which is asynchronous. The rest of the function runs in parallel with the AJAX request, so "num" is undefined before the $.post returns.

// num is undefined here... unless your network has 0 latency
$("#Layer7").html(num);

You need to wrap this code (pasted above) in a callback function. It appears to be parameter number 3: http://api.jquery.com/jQuery.post/

Untested, but best guess:

function search_buddy() {
    $.post("num.php", function (ret) {
        num=ret;
        $("#Layer7").html(num);
    });
}

Here are some modifications to help you understand:

function search_buddy() {
    alert("before the $.post execution");
    $.post("num.php", function (ret) {
        num=ret;
        $("#Layer7").html(num);
        alert("$.post callback");
    });
    alert("immediately after the $.post execution");
}

Note: "alert" will halt all JavaScript processing so you will be able to see what events occur when.

Oxyrubber
i read here http://www.webcheatsheet.com/javascript/variables.php that global variables do not need a "var" declaration,and hence i intended to declare num globally. And why does the code work fine with two or more characters ?
Anant
Your variable is implity global because you did not explicitly define it with "var". What this means is that your event handler _is_ executing on the first event... you just don't see any output because you change the HTML before you get the ASYNC response. Using my code should wrap the HTML changes in with the response code.
Oxyrubber
^^I'm sorry I don't understand. How is the HTML changed before the response ? The code $("#Layer7").html(num); is after the $.post() function isn't it ?
Anant
The $.post operation happens outside of this JavaScript "thread". Think of it as a forked operation. This is the "asynchronous" in "Asynchronous JavaScript and XML" (AJAX).
Oxyrubber
Frankly, I think the quirksmode page you link to is a pretty poor resource for key events. It's incomplete and doesn't explain the different purposes of the keydown/keyup versus keypress. In particular, it fails to explain in black and white terms that only `keypress` can be used to extra information about the character the user typed, which is a fact that most developers seem unaware of. Much, much better is http://unixpapa.com/js/key.html
Tim Down
@ user465089 : I'm sorry sir,I don't understand what you mean by "The $.post operation happens outside of this JavaScript "thread" ".
Anant
@Tim Down: Please keep on topic
Oxyrubber
@Anant: the $.post function is handled in parallel with all of the rest of the Javascript adjacent to it. $.post (1) makes an HTTP post to the server, (2) waits for the server response, (3) receives the response, and (4) runs the "callback" function (the one I wrote for you). In your code, $("#Layer7").html(num); runs at the same time (1) is happening. If you move this code into the "callback" function, you are assured that it happens during (4)
Oxyrubber
hmm,I'm starting to understand you. One general doubt : as $("#Layer7").html(num); is after $.post(),isn't it executed after $.post() ?
Anant
In general, yes. The difference is that $.post internally makes an xmlhttprequest, then continues processing the rest of the JavaScript below the $.post call. When the response is returned, your callback function is executed soon after. This is similar to what happens with a setTimeout (I'll update my original response with an example).
Oxyrubber
thanks a LOT! I really appreciate your help :)
Anant
Please don't forget to vote up / accept if you can.
Oxyrubber
@user465089: How was my comment not on topic? If I'd posted it as an answer it would have been off topic, but as a comment on an assertion in your answer that I disagreed with it was perfectly valid.
Tim Down
@Tim: Your resource did not directly help resolve the issue. We should take this offline if you want to discuss more.
Oxyrubber
@Oxyrubber: It's OK thanks, I'm done.
Tim Down