0

I'm guessing this should be simple, but I can't for the life of me find out how to do it.

I have an each loop, which runs through a number of products, and runs a function against each of them. This function, amongst other things, updates a table with the product's information. However, I need it to wait until 'runThisFunction()' completes, before moving on to the next product, and running the function again.

For example;

$.each(data.products, function(id, v) {
    runThisFunction(v);
});

Inside runThisFunction(), it pulls information about the product from the LocalStorage database, but because it runs so quickly, it moves on to the next product before it's done what it needs to do. I could add a delay, but I don't want to purposely slow it down.

I know this should be simple...

olimortimer
  • 1,373
  • 10
  • 23

2 Answers2

0

Have a look at the queue() function of jQuery. http://api.jquery.com/queue/

Another useful source: What are queues in jQuery?

Community
  • 1
  • 1
Pieter
  • 1,823
  • 1
  • 12
  • 16
0

As mentioned, further down in the runThisFunction(), it was accessing LocalStorage data for the product.

After much debugging, the issue was fixed by moving where the SQL statement variable was being defined, from outside of the db.transaction function, to inside.

Not quite sure why this was causing repercussions further up the code, but it's fixed the issue of the function within the $.each not firing synchronously.

olimortimer
  • 1,373
  • 10
  • 23