javascript - .each loop are not executed completely ... why ? ( code and log available ) -
javascript - .each loop are not executed completely ... why ? ( code and log available ) -
var idd;
$.each(data, function (i, items) { //1st each $.each(items, function (j, item) { // 2nd each console.log("field:" + j + " value:" + item); if (j=="sha_id"){ idd=item; } }); console.log("items"); console.log("id:" + idd); db.transaction(function (tx) { console.log("being inserted"); console.log("id inserting:" + idd); //then database insertion query
this sample upper part script
basically 1st .each run 2 round , 2nd .each run 2 round each 1st .each
so in console log expect see field .... value field ..... value items id : 2 beingness inserted id inserting : 2 field .... value field ..... value items id : 3 beingness inserted id inserting : 3
but getting
field .... value field ..... value items id : 2 field .... value field ..... value items id : 3 beingness inserted id inserting : 3 beingness inserted id inserting : 3
why db.transaction not executed after console.log("items") @ first in same loop. there code error should right ?
presumably db.transaction
asynchronous.
this means $.each
loops go on run, , don't wait db.transaction
complete.
when db.transaction
complete, callback gave invoked. happens (relatively) long after $.each
loops have finished.
the moral of story code should wait db.transaction
complete, must invoked within db.transaction
callback you're passing.
javascript jquery
Comments
Post a Comment