Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$.ajaxSetup({async:false}); stops mousemove event

Tags:

jquery

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

like image 392
Oleksandr IY Avatar asked Jan 19 '12 15:01

Oleksandr IY


2 Answers

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
});
like image 128
pimvdb Avatar answered Sep 24 '22 02:09

pimvdb


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.

like image 25
Richard Dalton Avatar answered Sep 24 '22 02:09

Richard Dalton