Javascript variable showing as undefined
<p>I'm struggling for a while now, I cannot find the reason why this code won't work.</p>

<p>When the #submitmsg is clicked it should trigger another function which gets time from a php file. This is sorta chat-box and it should display Name (which is Ja) in this case and time next to it which is varible "tajm". Though undefined is showing instead of time.</p>

<p>Any idea why doesnt this work?</p>

<p>Code snippet below:</p>

<pre><code>var tajm;

$(document).ready(function () {
    $("#submitmsg").click(function () {
        time();
        var newtext = $("#textmsg").val();
        var oldtext = document.getElementById("textbox").innerHTML;
        document.getElementById("textbox").innerHTML = oldtext + "&lt;br/&gt;&lt;div id='name'&gt;Ja:     " + tajm + "&lt;/div&gt;&lt;div id='red'&gt;" + newtext + "&lt;/div&gt;";
        $("#textbox").scrollTop($("#textbox")[0].scrollHeight);
    });
});

function time() {
    var ajax = new XMLHttpRequest();
    ajax.onreadystatechange = function () {
        if (ajax.readyState == 4) {
            tajm = ajax.responseText;
        }
    };
    ajax.open("GET", "r.php", true);
    ajax.send(null);
}
</code></pre>
