...

View Full Version : HELP: JavaScript Variable not Updating fast enough



puggi
09-16-2011, 05:31 PM
I just wrote some javascript with a counter to call an ajax call of many records, but when it is around the 50th record the counter variable is not updating in time for the next ajax call resulting in duplicate results cause ajax is pullin the previous query again

Old Pedant
09-16-2011, 05:43 PM
Sounds like you are using one set of global variables for your AJAX call instead of encapsulating it so each call is kept separate automatically.

But if you don't show us code, how can we know for sure?

puggi
09-16-2011, 06:04 PM
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html><head>

<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
<title>sql</title>
<script src="javascript/jquery.1.3.2.min.js" type="text/javascript"></script>

<script type="text/javascript">

var sql="SELECT COUNT( DISTINCT `StudentID` ) FROM `AIR`";
var numStudents = 0;
var studentNumber = 1;
var AwardYear="20062007";
var OPEID ="031121";
var insName="John Dewey College";
var GEind="Y";
var program;
var cip;
var sid;
var llre=" , , , ,";

var sql1="";

var last;
var first;
var dob;
var curr;
var admPer;
var title;
var cip;
var cl = "01";
var mdr = "N";
var ffel = "N";

var pabd="TBD";
var pabday="TBD";
var pas="TBD";
var paed="TBD";
var pla = "NA";

var anotherProgram = "N";
var otherProgram="";
var otherCIP="";
var clop="";
var pnop="";
var geop="";
var opoi="";
var snop="";

var debit=0;
var credit=0;
var ifa=0;
var fr="FieRes";
var studentNumber2= 1;

var result ="Award Year,\tStudent SSN,\tStudent First Name,\tStudent Middle Name,\tStudent last Name,\tStudent DOB,\tInstitutions OPEID,\tInstitutions Name,\tGE Program Indicator,\tProgram Name,\tCIP Code,\tCredential Level,\tMedical or Dental Internship or Residency,\tFFEL or Direct Loans,\tProgram Attendance Begining Date,\tProgram Attendace Begin Date For This Award,\tProgram Attendance Status,\tProgram Attendance End Date,\tPrivate Loan Amount,\tInstitutional Financing Ammount,\tField Rserved,\tEnroll in Another Program,\tCIP Code of Other Program,\tCredential Level of Other Program,\tProgram Name of Other Program,\tGE Program Indicator of Other Program,\tOPEID of Other Program Institution,\tSchool Name for Other Program\n";

function ajgetNumStudents(){
//alert("in aj-get");
try{
var sql = "SELECT COUNT( `StudentID` ) FROM `AIR`";
var url="/M-SMS/sqlGet3";
$.get(url,{sql:sql}, function(data){
//alert("amount of students: "+data);
numStudents = parseInt(data);
document.getElementById("result").value=result;
incNum1();

//ajgetGetSS();
});
}
catch(err){ alert(err);}
}

function ajgetGetSS(){
//alert("ajgetGetSS");
if(studentNumber < numStudents){
try{
if(studentNumber != studentNumber2){
//alert("studentNumber sid not increase by 1");
//studentNumber++;
}
//var sql1 = "SELECT `StudentID` FROM `AIR` WHERE `number` LIKE '"+studentNumber+"' LIMIT 0, 1";
//alert(sql1);
var url="/M-SMS/sqlGet3";
$.get(url,{sql:sql1}, function(data){
//alert("Student ID from AIR: "+data);
var re2 = document.getElementById("d1").innerHTML;
document.getElementById("d1").innerHTML = re2+"<h3>"+studentNumber+" "+data+" "+sql1+"</h3>";
sql1="";
sid=data;
setTimeout("ajgetInfoFromDemog()",1000);
});
}
catch(err){ alert(err);}

}else{alert("done");
}
}
function ajgetInfoFromDemog(){
//alert("ajgetInfoFromDemog(dat)");
var dat = sid;
try{
var sql = "SELECT `LastName`,`FirstName`,`Birthday`,`CurrentCurriculum`,`AdmissionPeriod` FROM `UT_Demografic` WHERE `StudentID` LIKE '"+dat+"' ";
var url="/M-SMS/sqlGet3";
$.get(url,{sql:sql}, function(data){
//alert("Data from Demog: "+data);


var params = data.split("*");
last = params[0];
first = params[1];
var ff=first.split(" ");
first = ff[0];
dob = params[2];
curr = params[3].split("-")[0];

sid = dat;
ajgetGetCIP();
//document.getElementById("result").value=re+"\n"+AwardYear+",\t"+dat+",\t"+first+",\tNA"+",\t"+last+",\t"+dob+",\t"+OPEID+",\t"+insName+",\t"+GEind+",\t"+curr;
});
}
catch(err){ alert(err);}
}
function ajgetGetCIP(){
//alert("ajgetGetCIP()");
try{
var sql = "SELECT `Title`,`CIPcode` FROM `CIPcodes` WHERE `schoolCode` LIKE '"+curr+"'";
var url="/M-SMS/sqlGet3";
$.get(url,{sql:sql}, function(data){
//alert("CIP-Info: "+data);
title=data.split("*")[0];
cip=data.split("*")[1];
setTimeout("ajgetGetIFA()",10);

});
}
catch(err){ alert(err);}
}
function ajgetGetIFA(){
//alert("ajgetGetIFA");
try{
var sql = "SELECT `Debit`,`Credit` FROM `U_Accounts` WHERE `StudentID` LIKE '"+sid+"'";
var url="/M-SMS/sqlGet3";
$.get(url,{sql:sql}, function(data){
//alert("DATA from ACC: "+data);
if(data == "no records"){
ifa=0;
debit = 0;
credit = 0;

}else{
var dc = data.split(";");
//alert(dc.length);

for (i=0;i<dc.length;i++){
var d = dc[i].split("*")[0];
var c = dc[i].split("*")[1]
//alert("d&c"+i+": "+d.substring(8,d.length)+d.substring(0,d.length-1)+" "+c.substring(8,c.length)+c.substring(0,c.length-1));
if(d.substring(8,d.length)=="+"){
//alert("adding to debit: "+parseFloat(d.substring(0,d.length-1)));
debit=debit+parseFloat(d.substring(0,d.length-1));
}else{
debit=debit-parseFloat(d.substring(0,d.length-1));
}
if(c.substring(8,c.length)=="+"){
credit=credit+parseFloat(c.substring(0,c.length-1));
}else{
credit=credit-parseFloat(c.substring(0,c.length-1));
}
//alert("debit"+debit);
//alert("credit"+credit);
//alert("total: "+(debit-credit));
}
//alert("last-total: "+(debit-credit));
ifa = (debit-credit);
debit = 0;
credit = 0;
}
ajgetGetAdmPerClass();
});
}
catch(err){ alert(err);}
}
function ajgetGetAdmPerClass(){
//alert("in ajgetGetAdmPerDate");
try{
var sql = "SELECT `Class` FROM `UT_Status` WHERE `StudentID` LIKE '"+sid+"' ";
//alert("sql: "+sql);
var url="/M-SMS/sqlGet3";
$.get(url,{sql:sql}, function(data){
//alert("pabd: "+data);
admPer=data.substring(0,(data.length-1));
ajgetGetAdmPerDate();
});
}
catch(err){ alert(err);}
}

function ajgetGetAdmPerDate(){
//alert("in ajgetGetAdmPerDate");
try{
var sql = "SELECT `BeginDate` FROM `UT_BeginDate` WHERE `Period` LIKE '"+admPer+"' ";
//alert("sql: "+sql);
var url="/M-SMS/sqlGet3";
$.get(url,{sql:sql}, function(data){
//alert("pabd: "+data);
pabd=data;
ajgetGetAdmPerDateAY();
});
}
catch(err){ alert(err);}
}

function ajgetGetAdmPerDateAY(){
//alert("in ajgetGetAdmPerDateAY");
try{
if(pabd != "no records"){
//alert(pabd.substring(0,2));
var month = parseFloat(pabd.substring(0,2));
var year = parseFloat(pabd.substring((pabd.length-4),pabd.length));
//alert("month: "+month+" year: "+year);
if((month < 7)&&(year==2006)){
//alert("if1");
pabday="07012006";}
else if((month >0)&&(year<2006)){
//alert("if2");
pabday="07012006";}
else{
//alert("else");
pabday=pabd}
//alert("pabd: "+pabd+" pabday: "+pabday);
}else{}

ajgetGetStatus();
}
catch(err){ alert(err);}
}

function ajgetGetStatus(){
//alert("ajgetGetStatus()");
anotherProgram="N";
otherProgram="";
otherCIP="";
clop="";
pnop="";
geop="";
try{
var sql = "SELECT `StatusDate`,`Status`,`Program` FROM `UT_Status` WHERE `StudentID` LIKE '"+sid+"' ORDER BY `StatusDate` ";
//alert("sql: "+sql);
var url="/M-SMS/sqlGet3";
$.get(url,{sql:sql}, function(data){
//alert("Status: "+data);


if(data == "no records"){
pas="E";
//alert("pas: "+pas);
}else{
//para escojer status de ano fiscal
//data = "20050428,RA,TRP;20060816,WF,TEP;20070116,RA,TEP";
if(data.indexOf(";") == -1){
var pp = data.split(",");
var stat = pp[1];
//alert("stat: "+stat);


//asignacion de letra
if((stat == "WF")||(stat == "WY")||(stat == "WH")||(stat == "WX")||(stat == "WN")||(stat == "CA")||(stat == "NS")||(stat == "W")){
pas="W";
paed=pp[0].substring(4,6)+pp[0].substring(pp[0].length-2,pp[0].length)+pp[0].substring(0,4);
}
else if((stat == "GD")){
pas="C";
paed=pp[0].substring(4,6)+pp[0].substring(pp[0].length-2,pp[0].length)+pp[0].substring(0,4);
}
else{ pas =="E";}
//final de asignacion de letra

//alert("pas: "+pas);
}



else{


var statuses = data.split(";");
//alert("statuses: "+statuses);
var statuses2 = new Array();
var s = 0;


for(i=0;i < statuses.length;i++){

var statDate = parseFloat((statuses[i].split(",")[0]));
if((statDate>20060701)&&(statDate<20070630)){
statuses2[s]=statuses[i];
s++;
}else{}

}
//alert("statuses2: "+statuses2);
if(statuses2.length < 1){
pas="E";
nextFunc();
}else{

var stat2 = statuses2[0].split(",")[1];
//alert("stat2: "+stat2);
//asignacion de letra
if((stat2 == "WF")||(stat2 == "WY")||(stat2 == "WH")||(stat2 == "WX")||(stat2 == "WN")||(stat2 == "CA")||(stat2 == "NS")||(stat2 == "W")){

pas="W";
var pppp=statuses2[0].split(",")[0];
paed=pppp.substring(4,6)+pppp.substring(pppp.length-2,pppp.length)+pppp.substring(0,4);

}
else if((stat2 == "GD")){
pas="C";
paed=pppp.substring(4,6)+pppp.substring(pppp.length-2,pppp.length)+pppp.substring(0,4);
}
else{ pas =="E";}
//alert("pas: "+pas);
//final de asignacion de letra

if(statuses2.length > 1){

var stat2 = statuses2[1].split(",")[1];
if((stat2="RA")||(stat2="AP")){
anotherProgram = "S";
clop="01";
geop="Y";
otherProgram= statuses2[1].split(",")[2];
}else{
anotherProgram = "N";
}
//alert("anotherProgram: "+anotherProgram+" otherProgram: "+otherProgram);
}else{}

}

} }



nextFunc();


});


}
catch(err){ alert(err);}
}

function nextFunc(){
//alert("in nextFunc()");
if(anotherProgram == "S"){

ajgetGetCIP2(otherProgram);

}else{
//document.getElementById("result").value=result;
var re = document.getElementById("result").value;
var lre = studentNumber+", "+AwardYear+",\t"+sid+",\t"+first+",\tNA"+",\t"+last+",\t"+dob+",\t"+OPEID+",\t"+insName+",\t"+GEind+",\t"+title+",\t"+cip+",\t"+cl+",\t"+mdr+",\t"+ffel+",\t"+pabd+",\t"+pabday+",\t"+pas+",\t"+paed+",\t"+pla+",\t"+ifa+",\t"+fr+",\t"+anotherProgram+",\t"+otherCIP+",\t"+clop+",\t"+pnop+",\t"+geop+",\t"+opoi+",\t"+snop+"\n";
//alert(lre.split(",")[2]);
if((lre.split(",")[2])!=(llre.split(",")[2])){
document.getElementById("result").value=re+lre;
llre=lre;
}else{}

setTimeout("incNum1()",1000);

}
}
function ajgetGetCIP2(cur2){
//alert("ajgetGetCIP2(cur2");
try{
var sql = "SELECT `Title`,`CIPcode` FROM `CIPcodes` WHERE `schoolCode` LIKE '"+cur2+"'";
var url="/M-SMS/sqlGet3";
$.get(url,{sql:sql}, function(data){
//alert("CIP-Info: "+data);
pnop=data.split("*")[0];
otherCIP=data.split("*")[1];


//document.getElementById("result").value=result;
var re = document.getElementById("result").value;
var lre = studentNumber+", "+AwardYear+",\t"+sid+",\t"+first+",\tNA"+",\t"+last+",\t"+dob+",\t"+OPEID+",\t"+insName+",\t"+GEind+",\t"+title+",\t"+cip+",\t"+cl+",\t"+mdr+",\t"+ffel+",\t"+pabd+",\t"+pabday+",\t"+pas+",\t"+paed+",\t"+pla+",\t"+ifa+",\t"+fr+",\t"+anotherProgram+",\t"+otherCIP+",\t"+clop+",\t"+pnop+",\t"+geop+",\t"+opoi+",\t"+snop+"\n";
//alert(lre.split(",")[2]);
if((lre.split(",")[2])!=(llre.split(",")[2])){
document.getElementById("result").value=re+lre;
llre=lre;
}else{}
setTimeout("incNum1()",1000);



});
}
catch(err){ alert(err);}
}

function incNum1(){
//alert("in inc1 studentNumber: "+studentNumber+" studentNumber2: "+studentNumber2);
if(studentNumber == studentNumber2){
studentNumber++;
setTimeout("incNum2()",1000);
}else{
studentNumber2 = studentNumber;
//alert("out inc1 studentNumber: "+studentNumber+" studentNumber2: "+studentNumber2);
sql1 = "SELECT `StudentID` FROM `AIR` WHERE `number` LIKE '"+studentNumber+"' LIMIT 0, 1"
if(sql1 == ""){setTimeout("incNum2()",1000);}else{
setTimeout("ajgetGetSS()",3000);}
}

}
function incNum2(){
//alert("in inc2 studentNumber: "+studentNumber+" studentNumber2: "+studentNumber2);
if(studentNumber == studentNumber2){
studentNumber++;
setTimeout("incNum1()",1000);
}else{
studentNumber2 = studentNumber;
//alert("out inc2 studentNumber: "+studentNumber+" studentNumber2: "+studentNumber2);
sql1 = "SELECT `StudentID` FROM `AIR` WHERE `number` LIKE '"+studentNumber+"' LIMIT 0, 1"
if(sql1 == ""){setTimeout("incNum1()",1000);}else{
setTimeout("ajgetGetSS()",3000);}
}

}

</script>

</head><body>
<table style="text-align: left; width: 1048px; height: 315px;" border="1" cellpadding="2" cellspacing="2">

<tbody>
<tr>
<td style="vertical-align: top; width: 328px;">sql:<input size="40" id="sql" name="sql"><br>
<br>
<button id="submit" name="submit" type="button" onclick="javascript:ajgetNumStudents();" >submit</button><br>
</td>
<td style="vertical-align: top; width: 700px;">Result:<br>
<div style=" WIDTH: 700px; HEIGHT: 280px;">
<textarea id="result" cols="80" rows="10" name="result" wrap="off" style="width:100%;height:100%;" ></textarea>
</div>
<br>
</td>
</tr>
</tbody>
</table>

<br>
<div id="d1">
</div>

<br>
</body></html>

puggi
09-16-2011, 06:12 PM
studentNumber variable is the unstable one

Old Pedant
09-16-2011, 09:46 PM
Ahhh...you didn't say you are using jQuery. I don't use jQuery. Maybe somebody else knows how to get jQuery to handle multiple concurrent AJAX requests, if that is indeed the problem.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum