html5 - jquery - how to prevent delayed execution of executeSql? -
i new jquery mobile.
my issue first, have value database (for qnnumber using fn togetquestionnumber) utilize value in xmlparser function.
if executed in proper order - alert should displayed in order - first, second, third.
but shown alert coming second, third, first. (means db function togetquestionnumber executed @ end!)
how create togetquestionnumber execute first - taht value of qnnumber - execute xmlparser?
//////////////////////////////////////////////////
$('#alpha').live('pageshow', function(event) { var day = geturlvars()["day"]; var module = geturlvars()["module"]; var qnnumber=1; var db = opendatabase("exam", "1.0", "the exam", 200`enter code here`000); var dataset; var selectallstatement = "select totqn, totanswd quizscore dayid=" + day + " , moduleid=" + module ; function togetquestionnumber() { db.transaction(function(tx) { tx.executesql(selectallstatement, [], function(tx, result) { dataset = result.rows; (var = 0, item = null; < dataset.length; i++) { item = dataset.item(i); //if(item['totanswd']>0) //{ qnnumber = item['totanswd']; alert('first=' +qnnumber); ////// first alert //} } }); }); } togetquestionnumber(); alert('second' + qnnumber); ////// sec alert var xml; $(document).ready(function(){ $.ajax({ type: "get", url: "data/quiz.xml", datatype: "xml", success: xmlparser }); }); //loading xml file , parsing .main div. function xmlparser(data) { xml = data; $('#list').empty(); alert('third' + qnnumber); ////// 3rd alert } $('#list').trigger('create'); });
////////////////////////////////////////////////////////
javascript never stop
togetquestionnumber(); homecoming directly, not wait tx.executesql
so, alert('second' + qnnumber); called first
second, on page ready callback ajax request, it's not place set ready callback
instead of doing
.live .ready
do
.ready .live
just take in mind javascript asynchron, there callback after action, $.ajax , tx.executesql
if want sec action called after first one, launch sec action on end callback of first one
try thing :
$(document).ready(function(){ $('#alpha').live('pageshow', function(event) { var day = geturlvars()["day"]; var module = geturlvars()["module"]; var qnnumber=1; var db = opendatabase("exam", "1.0", "the exam", 200`enter code here`000); var dataset; var selectallstatement = "select totqn, totanswd quizscore dayid=" + day + " , moduleid=" + module ; var xml; //loading xml file , parsing .main div. function xmlparser(data) { // if here ajax request end xml = data; $('#list').empty(); alert('third' + qnnumber); ////// 3rd alert $('#list').trigger('create'); } function togetquestionnumber() { db.transaction(function(tx) { tx.executesql(selectallstatement, [], function(tx, result) { dataset = result.rows; (var = 0, item = null; < dataset.length; i++) { item = dataset.item(i); //if(item['totanswd']>0) //{ qnnumber = item['totanswd']; alert('first=' +qnnumber); ////// first alert // if here, db request end ! alert('second' + qnnumber); ////// sec alert // phone call ajax request $.ajax({ type: "get", url: "data/quiz.xml", datatype: "xml", success: xmlparser }); //} } }); }); } // start togetquestionnumber(); }); });
jquery html5 mobile
No comments:
Post a Comment