Perform a function in jQuery only after the previous one has finished running in a cascading manner

Looking for guidance from an expert on how to accomplish this task. I have HTML code for a multiple choice question with options A to E:

<div id="correct-answer-XXXXX" style="display:none;"></div>
<div id="wrong-answer-XXXXX" style="display:none;"></div>

In addition, there is jQuery AJAX code that sends data to a remote URL (which I do not have access to) and manipulates the display of the divs based on whether the answer is correct or wrong.

var vector = new Array('A','B','C','D','E');
var i = 0;
function answerQuestion(XXXXX, choice){
    $.ajax({
        type:"POST",
        dataType:'jsonp',
        data:{choice},
        url:"http://myremoteurl.com/"+XXXXX,
        beforeSend:function(){
        },
        complete:function(){
            var style = $("#correct-answer-"+XXXXX).css("display");
            if (style == "block") {
                // Do something with the correct answer 
            } else {
                i++;
                answerQuestion(XXXXX, vector[i]);
            }
        }
    });
}

The above code works well for a single question, but I need to repeat this process sequentially 60 to 100 times. Once one question is answered, move on to the next.

I have a container filled with fields containing the data XXXXX, allowing me to iterate over them in a loop:

<div id="fieldscontainer">
<input type="hidden" name="111111" value="111111">
<input type="hidden" name="222222" value="222222">
<input type="hidden" name="333333" value="333333">
...
<input type="hidden" name="XXXXXX" value="XXXXXX">
</div>

To determine the correct answer, we must check the style of div correct-answer-XXXXX. A simple $.each loop won't suffice as the function needs to run multiple times until the right answer is found.

Illustration demonstrating the desired outcome:

Answer №1

Employ

sync : true

$.ajax({
    method:"GET",
    dataType:'json',
    sync : true, /// don't forget to include this line

I trust this information proves beneficial.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Guide to creating a custom wrapper for a Material UI component with React JS

I am utilizing the Material UI next library for my project and currently working with the List component. Due to the beta version of the library, some parameter names are subject to change. To prevent any future issues, I have decided to create a wrapper a ...

What is the best way to incorporate JQuery from the node_modules directory instead of using a CDN in a Laravel 5.8 application?

I am facing an issue with integrating JQuery from my npm packages into my Laravel app. In order to make Datatables and other functionalities work, I need JQuery to be properly set up. Although I have installed JQuery using npm, I am encountering an error w ...

Issue: The error message "undefined variable 'angular'" appears when attempting to access offline files stored on a network drive

I have successfully developed offline forms using angular js v1.6.4, angular-ui-bootstrap, and angular-ui-router without the need for server hosting. When the package is saved on local storage, it functions perfectly on both IE and Chrome browsers. Howeve ...

Tips for exporting data to a JSON file using the Google Play Scraper in NodeJS

Recently, I've been exploring the Google Play Scraper and I'm in need of a complete list of all appIds using NodeJS. The issue I'm facing is that it only outputs to console.log. What I really require is to save this output to JSON and then c ...

The completion event for Ajax and Json does not activate Google Maps

I'm currently facing an issue with my Google Maps functionality. Despite trying various solutions that involve hidden tabs, the problem persists. The trouble lies in the fact that my ajax function successfully posts and retrieves data, but fails to tr ...

Linking a Wordpress submenu to a specific section of a page

I am utilizing a Wordpress menu to act as the primary navigation for my website. Each main item in this menu is a Page Type that redirects to other existing pages, with one of them containing submenus made up of custom links. These custom links' URLs ...

Uneven animation when scrolling upwards on Safari and FireFox

I've recently been working on a website where I included a scroll animation that moves an icon image upwards depending on the offset position of the content in the right container. Although the logic is functioning properly, I'm experiencing some ...

Invoking a function without specifying its parent function when nested within another function

I need to execute an anonymous function within another function without actually calling the parent function, as it will lead to errors. Here is the parent function: function onloadCallback() { grecaptcha.render("recaptchaHolder", { "size": "invisi ...

No data being returned by $.get in Internet Explorer

I'm currently utilizing JQuery's $.get method to retrieve a twitter feed and showcase it on my website. Strangely, I am encountering an issue where no data seems to be fetched (i.e., the code within function(d) { ... } is not being executed). Thi ...

Tips for utilizing createTextNode in JSDOM

When attempting to create a UI test for an HTML element using Jasmine and jsdom, I encountered an issue. Within my component, I utilized the createTextNode function to populate the content of the DOM element. Unfortunately, during testing, the document.c ...

What steps are necessary to add a Contact Us form to my HTML website?

Currently, I am searching for a way to add a "Contact Us" section to my website that is completely HTML-based. I believe the best approach would involve using a PHP script to handle sending emails from a form on the Contact Us page, but I am not familiar ...

Choose three different images and one corresponding word from a JavaScript array to be displayed individually on the screen in separate div containers

I am in the process of developing a web game that will showcase 3 images on the screen, and the player must select the image that corresponds to the displayed word. I have successfully created a JavaScript array containing the images and words retrieved fr ...

Exploring the Differences: NodeJS and ExpressJS for Server-Side and Client-Side HTML Rendering

Embarking on my journey with Node.js, I find myself grappling with the concept of server-side and client-side HTML pages. My objective is to hone my skills by creating an e-commerce web store. The technology stack that I have set my sights on includes Node ...

Creating a visually dynamic stack of images using javascript, jquery, and HTML

I'm interested in creating a unique image viewer using javascript/jQuery/HTML that combines elements of a book page flip and iTunes coverflow, optimized for mobile device browsers. I've been searching for tutorials to help kickstart this project, ...

What is the best approach for creating a brute-force solution for finding the smallest change algorithm

Looking for a solution to this algorithm challenge: Create a function that can determine the minimum amount of change that cannot be made with the given coins. function nonConstructibleChange(coins) { coins = coins.sort((a, b) => a - b); // O(nlogn) ...

Modifying static content within jQuery tabs

Encountering an issue with jQuery $('#tabs').tabs();. When checking out the example on JSFIDDLE, I noticed that the content containing an external php file is always displayed, even when switching to other tabs. <li class="files"> < ...

Modifying script variables using the Chrome console

There is a button on the website that looks like this: https://i.sstatic.net/G7PBF.png Clicking on this button triggers the following script: https://i.sstatic.net/rIdLW.png function count(){ let downloadTimer; var timeleft = 30; downloadTimer = setInte ...

submitting two contact forms using ajax

Looking to enhance my knowledge of form design... Hello there! I'm currently working with two contact forms on the same page, both utilizing ajax for submissions. Each form functions properly when standalone, but as soon as I integrate them togethe ...

What is the reason behind the controller being unable to locate an Angular model when it contains dots in its name?

I am completely new to Angular and struggling to comprehend this code snippet. app.controller('FileConfigController-detail', function($scope, $http, $stateParams, FileConfigDetailsService) { $scope.detail.inptITResourceID = "test me" ...

What is the method for passing an element object instead of an id in JSXGraph?

I'm attempting to modify var brd2 = JXG.JSXGraph.initBoard('box', {boundingbox: [-8.75, 2.5, 8.75, -2.5]}); to read as var brd2 = JXG.JSXGraph.initBoard(this.$('#box'), {boundingbox: [-8.75, 2.5, 8.75, -2.5]}); Due to the creat ...