0

I am using async:false. Therefore in case of success function, it should wait till it gets a response from the function and should stop executing the code that follows the success function.

However it executes the code following it. Shouldn't async lock the process? Now in mycase, I am making $('#val1loading').text('loading') which has to keep showing loading until it gets a success from the ajax call. But it doesn't.

$("#val").change(function() {
$('#val1loading').text('loading');
if ($('#val').val() != '') {
 $.ajax({
 url: "Functions.php",
 dataType: 'json',
 data: {
 async: false,
 'link': $('#val').val()
 },
 success: function(result) {
 $("#val1").val(result[0]);
 },
 error: function(xhr, status, error) {
 $("#val1").val('');
 }
 });
} else {
 //some code
}
$('#val1loading').text('');

});

n4m31ess_c0d3r
3,1485 gold badges29 silver badges35 bronze badges
asked Nov 7, 2016 at 14:54
2
  • You can place the $('#val1loading').text(''); inside success right Commented Nov 7, 2016 at 14:57
  • isn't async: false, supposed to be outside of the data ? Commented Nov 7, 2016 at 15:03

3 Answers 3

2

The async: false should be in the main object passed to $.ajax, not contained within the data sub-object.

That said, using synchronous AJAX is almost always a bad idea precisely because it does "lock the process" as you mentioned in the question.

Get used to writing properly async code, where every action in the program happens via callbacks as a result of events (e.g. AJAX completion) or better yet use "Promises", e.g.:

$('#val').on('change', function() {
 var $val1 = $('#val1');
 var $loading = $('#val1loading');
 var val = this.value.trim();
 if (val.length > 0) {
 $loading.text('loading');
 $.ajax({
 url: "Functions.php",
 dataType: 'json',
 data: { link: val },
 }).then(function(result) {
 $val1.val(result[0]);
 }).fail(function(xhr, status, error) {
 $val1.val('');
 }).always(function() {
 $loading.text('');
 });
 }
});
answered Nov 7, 2016 at 14:57
Sign up to request clarification or add additional context in comments.

2 Comments

So could u suggest a solution?
@RamMalhotra I did, for your immediate problem. If you want to use async properly, there's thousands of other questions already here relating to that.
0

async is deprecated.Inorder to solve your problem use $('#val1loading').text('') in all the cases.

$("#val").change(function(){
$('#val1loading').text('loading');
if($('#val').val() != '') {
 $.ajax({
 url: "ajaxFunctions.php",
 dataType: 'json',
 data: {'update': 'parser','link': $('#val').val()},
 success: function (result) {
 $('#val1loading').text('');
 $("#val1").val(result[0]);
 },
 error: function(xhr, status, error) {
 $("#val1").val('');
 $('#val1loading').text('');
}
 });
 }else
{
 //some code
}
//$('#val1loading').text('');
answered Nov 7, 2016 at 14:57

3 Comments

async: false is not deprecated - however you can't mix it with Promises / Deferred. It's still a bad idea, though.
@Alnitak As of jQuery 1.8, the use of async: false with jqXHR ($.Deferred) is deprecated; you must use the success/error/complete callback options instead of the corresponding methods of the jqXHR object such as jqXHR.done().So I have written.
and 1. not removed async: false from the data object, 2. repeated chunks of code that should have gone into a complete: handler (which is called regardless of success or error)
0

i think dont use async false like this. change it to :

 $.ajax({
 url: "Functions.php",
 dataType: 'json',
 data: {'link': $('#val').val()},
 async:false,
 success: function (result) {
 $("#val1").val(result[0]);
 },
 error: function(xhr, status, error) {
 $("#val1").val('');
 }
 });
answered Nov 7, 2016 at 14:57

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.