CPAN::Forum
CGI-Ajax - Events sometimes sent out of sequence
| Posted on Thu Jun 7 08:20:59 2007 by matthew |
| Events sometimes sent out of sequence |
|
I have noticed that when browser events happen extremely closely to one another that they can sometimes be sent out of sequence. I have the following program which attempts to replicate a dynamic search box:
use CGI ;
use CGI::Ajax ;
$now = scalar localtime ;
my $cgi = new CGI ;
my $pjx = new CGI::Ajax( 'exported_func' => \&perl_func );
my @data = qw(
absences
..... contents of data array snipped for brevity
..... I can send the full list if you like
worried
written
) ;
#$pjx->JSDEBUG(2) ;
#$pjx->DEBUG(1) ;
print $pjx->build_html( $cgi, \&Show_HTML);
sub perl_func {
my $search = shift ;
my $output .= "Remote search term was $search<br><br>" ;
my @matches = grep(/^${search}/,@data) ;
my $listbox_size = scalar @matches < 20 ? scalar @matches : 20 ;
$output .= '<select name="users" id="users" size="'.$listbox_size.'" style="width:440px;">' ;
foreach $match (@matches) {
$output .= "<option>$match</option>" ;
}
$output .= '</select>' ;
#print STDERR "Perl func returned\n" ;
return( $output );
}
sub Show_HTML {
$html = <<EOHTML ;
<html>
<head>
<title>This page generated by Perl</title>
</head>
<body>
Type a search term:
<input id="text1" type="text"
onkeyup="exported_func(['text1'],['resultdiv']) ; localdiv.innerHTML = 'Local search ter
+m is ' + text1.value ;">
<br><br>
<div id="resultdiv"></div><br><br>
<div id="localdiv"></div>
</body>
</html>
EOHTML
return $html ;
}
The results from the Ajax call are stored in resultdiv, while the actual browser events are stored in localdiv (ie this is used to compare the browser events to the events received by the Ajax function). What happens is if you type a string in the text box (say, 'ab') and then rapidly correct the string by backspacing and typing a new letter (so that you get 'a', and then rapidly 'ax' in the text box) then you will sometimes find the events arriving out of sequence at the server, ie the value 'ax' arrives before the value 'a' meaning that the search box shows words starting with 'a' rather than 'ax'. I have verified this by using the live headers function of Firefox which shows the events sent by the browser and the responses received (which I can send you if you like). I have found that this occurs with both Firefox and IE. |
| Write a response |
(8)
]