JavaScript Let code wait untill AJAX request is done code vein wait until installation is complete,arduino code wait

if(error == false){
    alert('username does not exist');
}else{
    alert('username exists');
};

Currently your above code is on top of javascript, it is always being executed whether you call your ajax or not.so put it into other function and call it after calling checkUsername().

function checkUsername(username,callback){
    $.ajax({
        url: '/check_username.php',
        type: 'post',
        data: {username: username},
        dataType: 'json',
        success : function(data){
            switch(data.response){
                case 0:
                    callback(true);
                break;
                case 1:
                    callback(false);
                break;
            }
        }
    });                 
};

var error;  // 'undefined' for now

checkUsername('abc',function(data){
    if(data == false){
        // username exists
        error = true;
    }else{
        // username does not exist
        error = false;
    };
    alertMsg();
});

function alertMsg(){
if(error == false){
    alert('username does not exist');
}else{
    alert('username exists');
}
}
Answer:1

try with async: false on ajax function

async (default: true) Type: Boolean By default, all requests are sent asynchronously (i.e. this is set to true by default). If you need synchronous requests, set this option to false. Cross-domain requests and dataType: "jsonp" requests do not support synchronous operation. Note that synchronous requests may temporarily lock the browser, disabling any actions while the request is active. 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() or the deprecated jqXHR.success().

Answer:2

Set async to FALSE and it will not move until ajax request is completed.

function checkUsername(username,callback){
    $.ajax({
        url: '/check_username.php',
        type: 'post',
        data: {username: username},
        dataType: 'json',
        async : false,
        success : function(data){
            switch(data.response){
                case 0:
                    callback(true);
                break;
                case 1:
                    callback(false);
                break;
            }
        }
    });                 
};

var error;  // 'undefined' for now

checkUsername('abc',function(data){
    if(data == false){
        // username exists
        error = true;
    }else{
        // username does not exist
        error = false;
    };
});

if(error == false){
    alert('username does not exist');
}else{
    alert('username exists');
};
Answer:3

I had the same problem couldn't find any way but this worked for me.

function sendAjax(){
var defObj = $.Deferred();
//my ajax code here  
defObj.resolve(ajaxResponse);
return defObj.promise();
}
Answer:4

AJAX (and a lot of other functionality of JavaScript) works asynchonous, this means you do not wait for something to happen but rater tell JavaScript what to do when something happens. In your example you're trying to wait 'till the AJAX-Request finished but what you should do is tell JavaScript to execute your code when the request finished, this means, put the code in the callback.

Answer:5

I have an asp.net textbox which also works with jquery datepicker. <asp:TextBox ID="tbxFirstReceivedDate" CssClass="selectstyle" runat="server" MaxLength="100" fieldtype='date' Width="200"><...

I have an asp.net textbox which also works with jquery datepicker. <asp:TextBox ID="tbxFirstReceivedDate" CssClass="selectstyle" runat="server" MaxLength="100" fieldtype='date' Width="200"><...

  1. convert string datetime to datetime python
  2. convert string datetime to date python
  3. convert string datetime to datetime c#
  4. convert string datetime to date in java 8
  5. convert string datetime to datetime
  6. convert string datetime to date in c#
  7. convert string datetime to datetime in sql server
  8. convert string datetime to datetime sql
  9. convert string datetime to date java
  10. convert string datetime to datetime java
  11. convert string datetime to long in java
  12. convert string datetime to instant java
  13. convert string datetime to datetime format c#
  14. convert string datetime to utc c#
  15. convert string datetime to localdatetime in java
  16. convert string datetime to datetime in javascript
  17. convert string datetime to timestamp java
  18. convert string datetime to calendar in java

In javascript I need to check whiwh value user enter in an html input like this: <input type="text" id="volume" class="form-control" name="voilume"></input> <div id"button"></...

In javascript I need to check whiwh value user enter in an html input like this: <input type="text" id="volume" class="form-control" name="voilume"></input> <div id"button"></...

Lets say I have a reducer which is like : const initialState = [ { accessToken: null, isLoggedIn: false, } ] export default function my_reducer(state = initialState, action) { switch (...

Lets say I have a reducer which is like : const initialState = [ { accessToken: null, isLoggedIn: false, } ] export default function my_reducer(state = initialState, action) { switch (...

  1. react redux merge state
  2. react redux mergeprops
  3. react redux mergeprops example
  4. react redux merge
  5. react-redux-form merge
  6. redux merge state

I have a javascript function that display and hide a div after select. The div contains fields of a form. The div test2 has the fields of the div test1 plus other fields. All the fields are required. ...

I have a javascript function that display and hide a div after select. The div contains fields of a form. The div test2 has the fields of the div test1 plus other fields. All the fields are required. ...

  1. sap display hide field
  2. sharepoint display form hide field