I try to create an MS-Dos emulation window with jquery and ajax. It works well but when I type a word or press enter, the script is one character too late on display (no character displayed on first press then, for each next keydown, it shows the previous character typed by the user instead of the current one).
Here is a simplified version of my script:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/terminal.css">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
//set cursor position
$('#cursor').css('left','0px');
//focus on textfield
$('#field').focus();
//On key down...
$('#field').keydown(function(event)
{
request=$(this).val();
count=$(this).val().length;
//Display chars as they come
$("#instruct").html(request);
// If "enter" is pressed
if (event.which == 13)
{
event.preventDefault();
//clean user entry
request=request.replace(/\n/g, "");
// Process user entry
$.ajax({
type: "POST",
url: "scripts/ajax/terminal.php",
data: {command:request},
success: function(response)
{
// if user typed something before pressing "enter"
if(response !='')
{
// update list by logging the previous entry, showing system response and clearing the textarea
$('#log').append("<li class='user'>--- ><span>"+request+"</span></li>");
$('#log').append("<li class='sys' >SYS ><span>"+response+"</span></li>");
$('#field').val('');
}
}
});
}
});
});
</script>
</head>
<body>
<ul id="log">
<li class='sys'>Sys ><span>Identification required</span></li>
</ul>
<ul id="realtime">
<li id="live-space">
<span class="user-line">--- ></span>
<textarea type="text" id="field"></textarea>
<div>
<span id="instruct"></span><div id="cursor">_</div>
</div>
</li>
</ul>
</body>
</html>
I'm sure "keyDown" is fired when it should because when I change this
$("#instruct").html(request);
into this
$("#instruct").html(request+'x');
...the "x" will show up directly after the first keydown while the "request" content will be delayed to the next keydown (for instance, if I type "a", I will only see "x" and when I type "b" immediatly after, I see "ax", etc...).
So it's not about firing the keydown but about picking the right character at the right time.
What do I do wrong?
Thank you.