Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8301671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:05:19+00:00 2026-06-08T17:05:19+00:00

I need to revert an object based on an AJAX call that I make.

  • 0

I need to revert an object based on an AJAX call that I make. However, I’m finding that the AJAX call does not finish before the function is finished executing.

I’ve tried various approaches but each one seems to come back to the same asynchronous problem – and I do not want to do a synchronous call.

How do I somehow wait until the AJAX call is complete and the answer is there before I move on to determine if I should revert or not?

$(document).ready(function() {

$.ajax
({
    type: "GET",
    url: "quizData.xml",
    dataType: "xml",
    success: function(xml)
    {
        var questionIdCount = 0;
        $(xml).find('question').each(function()
        {
            questionIdCount++;
            $("#question-container").append("<div class=\"question\" id=\"question-number-" + questionIdCount + "\">" + $(this).text() + "</div>");

            $("#question-number-" + questionIdCount).data('id', questionIdCount);

            $(".question").droppable(
            {

            }
            );

        });

        var answerIdCount = 0;
        $(xml).find('answer').each(function()
        {
            answerIdCount++;
            $("#answer-container").append("<div class=\"answer-id\" id=\"answer-number-" + answerIdCount + 
            "\">" + answerIdCount + "</div><div class=\"answer\">" + $(this).text() + "</div>");    

            $("#answer-number-" + answerIdCount).data('id', answerIdCount);

            $(".answer-id").draggable({
              revert: function(socketObj)
              {
                 if(!socketObj)
                 {
                     return true;
                 }
                 else
                 {
                    var questionId = $("#" + socketObj.attr('id')).data('id')
                    var answerId = $("#" + $(this).attr('id')).data('id');

                    return checkAnswer(questionId, answerId);
                 }


              },
              containment: "#containment-wrapper",
              scroll: true
            });


        }); 
    }
});
  $(document).ready(function() {

$.ajax
({
    type: "GET",
    url: "quizData.xml",
    dataType: "xml",
    success: function(xml)
    {
        var questionIdCount = 0;
        $(xml).find('question').each(function()
        {
            questionIdCount++;
            $("#question-container").append("<div class=\"question\" id=\"question-number-" + questionIdCount + "\">" + $(this).text() + "</div>");

            $("#question-number-" + questionIdCount).data('id', questionIdCount);

            $(".question").droppable(
            {

            }
            );

        });

        var answerIdCount = 0;
        $(xml).find('answer').each(function()
        {
            answerIdCount++;
            $("#answer-container").append("<div class=\"answer-id\" id=\"answer-number-" + answerIdCount + 
            "\">" + answerIdCount + "</div><div class=\"answer\">" + $(this).text() + "</div>");    

            $("#answer-number-" + answerIdCount).data('id', answerIdCount);

            $(".answer-id").draggable({
              revert: function(socketObj)
              {
                 if(!socketObj)
                 {
                     return true;
                 }
                 else
                 {
                    var questionId = $("#" + socketObj.attr('id')).data('id')
                    var answerId = $("#" + $(this).attr('id')).data('id');

                    return checkAnswer(questionId, answerId);
                 }


              },
              containment: "#containment-wrapper",
              scroll: true
            });


        }); 
    }
});


function checkAnswer(questionId, answerId)
{
     var ajaxAnswer;
     $.ajax
     ({
        type: "POST",
        url: "getQuizAnswers.php",
        dataType: "xml",
        data: "questionId=" + questionId + "&answerId=" + answerId,
        success: function(xml)
        {
            console.log($(xml));
            $(xml).find("answer").each(function()
            {
                //alert($(this).text());
                if($(this).text() == "true")
                    ajaxAnswer = true;
                else
                    ajaxAnswer = false;

            });             
        }
      })

    return ajaxAnswer;  
}



});


});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T17:05:23+00:00Added an answer on June 8, 2026 at 5:05 pm

    Spencer,

    You should be able to control things with a deferred object, together with a reorganisation of the code to ensure that draggable is only set once the ajax within checkAnswer has responded.

    I’m not 100% confident in every aspect of this but here goes :

    $(document).ready(function() {
        function setDraggable(revert) {
            $(".answer-id").draggable({
                revert: revert
                containment: "#containment-wrapper",
            });
        }
        $.ajax({
            type: "GET",
            url: "quizData.xml",
            dataType: "xml",
            success: function(xml) {
                var questionIdCount = 0;
                $(xml).find('question').each(function() {
                    questionIdCount++;
                    $("#question-container").append("<div class=\"question\" id=\"question-number-" + questionIdCount + "\">" + $(this).text() + "</div>");
                    $("#question-number-" + questionIdCount).data('id', questionIdCount);
                    $(".question").droppable({});
                });
                var answerIdCount = 0;
            $(xml).find('answer').each(function() {
                answerIdCount++;
                $("#answer-container").append("<div class=\"answer-id\" id=\"answer-number-" + answerIdCount + "\">" + answerIdCount + "</div><div class=\"answer\">" + $(this).text() + "</div>");
                $("#answer-number-" + answerIdCount).data('id', answerIdCount);
                if(socketObj) {
                    var questionId = $("#" + socketObj.attr('id')).data('id')
                    var answerId = $("#" + $(this).attr('id')).data('id');
                    $.when(checkAnswer(questionId, answerId)).done(function() {
                        setDraggable(true);
                    }).fail(function() {
                        setDraggable(false);
                    });
                }
                else {
                    setDraggable(true);
                }
                scroll: true
            });
            }
        });
        function checkAnswer(questionId, answerId) {
            var ajaxAnswer = true;//remains true unless an incorrect answer is received
            var def =  new $.Deferred();
            $.ajax({
                type: "POST",
                url: "getQuizAnswers.php",
                dataType: "xml",
                data: {"questionId":questionId, "answerId":answerId},
                success: function(xml) {
                    console.log($(xml));
                    $(xml).find("answer").each(function() {
                        ajaxAnswer = ajaxAnswer && ($(this).text() == "true");
                    });
                    if(ajaxAnswer) 
                        def.resolve();//success
                    else 
                        def.reject();//failure
                },
                error: function(){
                    def.reject();//assume failure if ajax fails
                }
            });
            return def;//IMPORTANT!!
        }
    });
    

    This is moderately tricky and needs to be explained …

    Starting with checkAnswer, you will see that it now returns a deferred object, which becomes either resolved or rejected at some point in the near future (when “getQuizAnswers.php” responds).

    Back in the .find('answer').each(...) loop, you will see that that the code has been turned inside-out. Now, instead of calling checkAnswer() within the draggabe, draggable is set in response to checkAnswer (more specifically, in response to checkAnswer’s deferred being resolved or rejected).

    As the draggable needs to be set under several conditions, the draggable code has been moved into a utility function, thus avoiding duplication. You may need to pass more parameters into this function to control its behaviour more specifically (in particular the jQuery selector).

    This may need tweeking/debugging and there may be a simpler approach but this is what immediately comes to mind.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using svn and on occasion I need to revert some changes that
Need a map reduce function by mongo in php This my mongo structure [_id]
Need help, function getFamily() { FB.api('/me/family', function(response) { alert(JSON.stringify(response)); }); } With the above
Need help with a query that I wrote: I have three tables Company id
I need to revert back to the default option on a form on a
I need to revert local changes for deployments. (I'd used svn revert for this
I messed up on my SVN repository and now need to revert the entire
I need to selectively revert a few lines in a massive file (to be
I need to patch a system in production however I have already change the
I just deleted a folder in my SVN that I realized I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.