Skip to content
Advertisement

JavaScript/jQuery: How to run a function after an each() loop has finished?

I have the following. What is basically happening is I’m prefilling all the dropdowns/select options. I then retrieve the data for the current record and select the appropriate value for each dropdown/select option.

JavaScript

My question is how I can ensure that get_record_data(); is run after the loop has finished? As you can see I make POST requests within the loop so what I am finding is that sometimes these don’t finish before get_record_data(); is called.

I did try:

JavaScript

However since get_record_data(); is doing an AJAX request as well, I am finding it just goes into an infinite loop

JavaScript

Any suggestions? I need support for IE11 as well.

Thanks.

Advertisement

Answer

Know how many elements are in dropdowns_sql, make a count var and include the get_record_data call within the done function for the last element.

Something like:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement