I'm writing some jQuery code that performs a plain ajax call. I've a global Javascript variable that I need to increase thread-safely with every call. If this was Java, I would use synchronized
but this is not the case :-)
Here's a code snippet:
var myval;
function myFunc()
{
$.ajax({
url: myurl,
type: 'GET',
data: { ...my data... },
success: function()
{
myval++;
}
});
}
Given that myFunc()
is associated with a click event, how can I be sure that myvar
is always safely/consistently increased? I know I could maybe use async: false
, but I'd prefer to avoid it. I'm not even sure it would work that way.
Thanks for any help!