K
kj
Hi. I'm new to programming with XMLHttpRequest and I'm unclear
about how non-blocking requests are handled, and in particular
about the question of the potential for race conditions. Suppose
that I have a function like this one that gets called when, for
example, the user clicks some button X:
function handleButtonXClick() {
var request = makeRequest();
function handleResponse() {
if ( request.readyState == 4 && request.status == 200 ) {
// has its way with some Innocent Global Variables...
}
};
request.onreadystatechange = handleResponse;
var url = 'http://myurl.com/cgi/latestinfo';
request.open( 'GET', url ); // non-blocking request, asynchronous response
request.send();
}
My question is: if the user triggers two invocations of handleResponse
in rapid succession, could the second invocation begin before the
first one is finished? Or does the browser ensure that these two
invocations are executed sequentially?
Assuming that the browser allows both invocations to execute
concurrently, does JavaScript provide any mechanism for locking
shared resources, such as global variables?
(I know that I could design the UI to prevent concurrent requests,
e.g. by disabling button X until handleResponse finishes, but I
would like to avoid this if I can.)
Thanks!
kj
about how non-blocking requests are handled, and in particular
about the question of the potential for race conditions. Suppose
that I have a function like this one that gets called when, for
example, the user clicks some button X:
function handleButtonXClick() {
var request = makeRequest();
function handleResponse() {
if ( request.readyState == 4 && request.status == 200 ) {
// has its way with some Innocent Global Variables...
}
};
request.onreadystatechange = handleResponse;
var url = 'http://myurl.com/cgi/latestinfo';
request.open( 'GET', url ); // non-blocking request, asynchronous response
request.send();
}
My question is: if the user triggers two invocations of handleResponse
in rapid succession, could the second invocation begin before the
first one is finished? Or does the browser ensure that these two
invocations are executed sequentially?
Assuming that the browser allows both invocations to execute
concurrently, does JavaScript provide any mechanism for locking
shared resources, such as global variables?
(I know that I could design the UI to prevent concurrent requests,
e.g. by disabling button X until handleResponse finishes, but I
would like to avoid this if I can.)
Thanks!
kj