"When using ajax, make sure to set async to true and work with deferred

After struggling for 3 hours trying to fix an issue, I turned to the internet for help. Can someone please assist me with the following challenge:
How can I create a loop of AJAX requests that continues to run until the data received from the AJAX call is equal to "stop" while using while and setting async to true?

Here is an unsuccessful example of what I have tried:

do {
  promise = json('json.php');
  promise.success(function again(data) {
    if(data === 'stop') {
      return false;
    } else {
      console.log('data');
    }  
  });
} while (again()); 


function json(url) {  
   return $.ajax({  
     type: "GET",  
     dataType: 'text',  
     url: url
   });
}

Answer №1

function checkData(data) {
    if (data !== 'stop') {
        alert(data);
        sendRequest();
    }  
}

function sendRequest() {
    retrieveJsonData(location.href).then(checkData);
}

function retrieveJsonData(url) {  
    return $.ajax({  
        type: 'GET',  
        dataType: 'text',  
        url: url
    });
}

sendRequest();

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

Troubleshooting: jqGrid's reload function is not functioning properly

I am encountering an issue when trying to reload the grid using trigger('reloadGrid'). I make an AJAX call to the server and the server returns the xmlstring successfully, however, the grid does not update with the new data. Below is the code sni ...

Is it possible to retrieve the index of a particular element within an array during an update operation in MongoDB?

After executing the following update statement const result = await Post.updateOne({_id: postId},{ $pull: {reacts: {publisher: req.query.publisher}}, $inc: {postPoints: - reactsEnum[ReactType]} }); I am interested in obtaining the ...

Using AngularJS to show content based on a callback function

I'm a beginner in angular and it seems like I might be overlooking something. For the registration form, I need users to provide their location. Depending on whether they allow/support navigator.geolocation, I want to display a drop-down menu for cho ...

Tips for effectively incorporating additional directives into a directive as it is being defined

I'm encountering a significant issue with dynamic directives in angularjs. My goal is to include new directives to a directive while defining it through an object: compile: function () { return { pre: function (scope, iElement, iAttrs) { ...

Data tables failing to display accurate data

I have implemented Datatable for a table with eight identical columns and three columns that vary based on the selected radio button. The table is dynamically filled using an ajax call to a python function and the returned data is added to the table body. ...

Error: The system encountered an issue while trying to access an undefined property 'find'

I've been working on developing the backend for a wishlist feature, but I've encountered an issue with my code. (node:19677) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'find' of undefined Despite trying to redefi ...

How to retrieve specific items from an array contained within an array of objects using Express.js and MongoDB

Within the users array, there is an array of friends. I am looking to retrieve all friends of a specific user based on their email where the approved field is set to true. In my Node.js application, I have defined a user schema in MongoDB: const UserSchem ...

Is it possible for the children of a Three.js scene to have matrix positions that differ from the children of those children?

I am facing an issue with my ferris wheel. The baskets on the wheel are not aligning correctly when the scene is rotated: https://i.sstatic.net/Ek1mT.png Everything functions as intended until the scene is rotated, causing the baskets to misalign with th ...

What causes Node.js to be unable to handle requests from Vue.js?

I'm encountering a strange error where Node.js is unable to see the URL address and consistently returns a 404 error. In my Vue.js application, I am making a post request using the axios package when the user clicks a button. The code snippet shows t ...

`Adjusting function calls based on the specific situation`

On my webpage, I have implemented a tab system where clicking on a tab displays the corresponding content below. This functionality is controlled by a JavaScript/jQuery function called 'changeTab'. Now, I want to set up individual JavaScript fun ...

Error: The text value is not defined

Hello, I am currently working on retrieving the selected value of an input when the input is focused with the tab key. Below is my code: HTML <input id='texteb' type="text" value='' /> <button> click </button> JS ...

In what scenarios does Element.getClientRects() provide a collection of multiple objects as a return value?

Every time I use Element.getClientRects(), it always gives me a collection containing just one DOMRect object. Under what circumstances does Element.getClientRects() return a collection with multiple DOMRect objects? function handleClick() { console. ...

Remove all HTML tags except for those containing a specific class

Looking for a regex that removes all HTML tags except the "a" tags with the "classmark" class For example, given this HTML string: <b>this</b> <a href="#">not match</a> <a href="#" target="_blank" ...

Issues with AngularJS have arisen, as it is currently unable to recognize and access variables as needed

As a newcomer to web design and Angular, I am trying to replicate this example, where I consume a RESTful web service and use AngularJS to display the JSON data. However, I'm facing issues with my implementation. Here's how my main.jsp file look ...

I'm trying to figure out how to retrieve data from a JQuery autocomplete response that contains an array. Specifically, I want

https://i.stack.imgur.com/YpuJl.pngThis form field is for entering text input. <span> <img src="images/author2.jpg" width="50" /> //The profile picture displayed here is static but in the database I have a dynamic image. <input class="sea ...

Unique Fragments with AstroJS

Recently delving into learning AstroJS, I stumbled upon some intriguing templates on GitHub. One thing that caught my attention was the <Fragment> tag which seemed to be related to directives based on the astro documentation. Below is a snippet of th ...

Incorporating an unique identification code within the input field

I have a HTML code that displays geolocation information: <span id="currentLat"></span>, <span id="currentLon"></span> When combined with JavaScript, it works perfectly. However, I am trying to display the above value in a text fi ...

Tips on expanding the row number column width in jqGrid

Within my JQuery Grid, I am utilizing a parameter called rownumbers. jQuery("#dateInfo").jqGrid({ url : theURL, datatype : "json", sortable: false, colNames:['Date & Time'], colModel:[{name:'arrivalTime',index:'arrivalTime& ...

BookshelfJS: Establishing a One-to-One Relationship

Within my database, I am working with two tables - User and Address. The User table consists of the following two methods: shippingAddress: function() { var Address = require(appRoot + '/config/db').model('Address'); return thi ...

Transforming an Ext.data.TreeStore data structure into a JSON format

How can I convert an Ext.data.TreeStore to a string for saving to Local Storage? I tried using Ext.encode() but it's giving me a circular structure error. Has anyone encountered this issue and found a workaround? ...