Hi, my name is Dan and I am still a bit new to working with Javascript. I have some code where I want each div.extractedGroup to send its .html() through $.ajax and then wait for the response before moving on to the next group in the loop.
I think I may be making this more complicated than it needs to be. Can anyone help me figure out how to modify my code to achieve this?
So far, I've attempted to use a variable that changes between true and false based on the status of the ajax call (beforeSend and success), along with a setTimeout function within the success handler. I also tried wrapping the $.ajax call in a setTimeout function but haven't had any luck with either approach.
Here's the javascript code snippet:
$('#processGroup').on('click', function(event) {
event.preventDefault();
$('.extractedGroup').each(function(index, el) {
data = $(this).html();
request = $.ajax({
url: 'wait.php',
type: 'POST',
dataType: 'json',
data: {
urlstoparse: data
},
beforeSend: function() {
console.log('sending: ' + data);
// Next in $.each should wait until success before processing the next group
},
success: function(res) {
console.log(res);
// Now the next group in $.each can be processed
}
})
.done(function() {
console.log("success");
})
.fail(function(res) {
console.log(res);
})
.always(function() {
console.log("complete");
});
});
});
And here is the corresponding HTML:
<div id="responseResult" style="">
<div class="extractedGroup">www.pinterest.com/pin/504262489497532154/,www.pinterest.com/pin/355080751843289362/,www.pinterest.com/pin/294704369339289320/,www.pinterest.com/pin/300685712590894312/,</div>
<div class="extractedGroup">www.pinterest.com/pin/555068722796876871/,www.pinterest.com/pin/69805862945258757/,www.pinterest.com/pin/94575660900159173/,www.pinterest.com/pin/521221356846916476/,</div>
<div class="extractedGroup">www.pinterest.com/pin/197173289911047820/,www.pinterest.com/pin/413486809511385544/,www.pinterest.com/pin/355080751843289327/,www.pinterest.com/pin/53691420527287022/,</div>
<div class="extractedGroup">www.pinterest.com/pin/135882113732404986/,www.pinterest.com/pin/464222674063278838/,www.pinterest.com/pin/339318153145966062/,www.pinterest.com/pin/31103053648675435/,</div>
<div class="extractedGroup">www.pinterest.com/pin/414542340674052776/,www.pinterest.com/pin/65583738298561215/,www.pinterest.com/pin/497718196292156699/,www.pinterest.com/pin/101753272800833432/,</div>
<div class="extractedGroup">www.pinterest.com/pin/421157002626993183/,www.pinterest.com/pin/43628690112051613/,www.pinterest.com/pin/414542340674052770/,www.pinterest.com/pin/220957925438000313/,</div>
<div class="extractedGroup">www.pinterest.com/pin/462322717970755136/,</div>
If anyone has any suggestions or can point out what I might be doing wrong, your help would be greatly appreciated. Thank you!