This code stops mousemove event
$.ajaxSetup({async:false});
$.get(url).success(function(data) { result = data; });
Is there a way to avoid this but keep $.ajaxSetup({async:false});, like a load function?
Thanks
Additions:
what if I need
$.ajaxSetup({async:false});
$.get(url).success(function(data) { result = data; });
$.ajaxSetup({async:true});
return result
how to handle that?
Thanks
return
implies synchronous code flow. AJAX is asynchronous without async: false
, however. This does not mean you should use async: false
. Rather, you should change your code flow to use callbacks instead of return values.
This is needed since there is no way to make the asynchronous code synchronous (i.e. you can't use return
with asynchronous functions). The only way is to provide a callback yourself as well:
function get(url, callback) {
$.get(url).success(function(data) { callback(data); });
}
Like:
get("...", function(result) {
// use `result` which you normally got by assigning the return value
// of `get` to a variable
});
You should find a way to do what you want without setting async:false
.
Using the callbacks from your ajax request you should be able to do most things.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With