PDA

View Full Version : Racing condition help



Jahren
06-18-2009, 02:15 PM
hey guys,

I have 3 select boxes,
when you choose an option from the first, the second select gets populated based on the first choosen option. Same goes for the third select.

when they submit the data, I want to put back the values in those select based on the URL string. All goes well except for a racing condition.

I tried using a mutex and / or setTimout without success. it always results in an infinite loop.

Using alerts to debug tells me that mutex is true then becomes false when Ajax call has ended.

I know this is some epik javascript needs but I need to know the inner working of this language ;)
thanks

js1.js :


var mutex = false;

function doSomethingAjax()
{
mutex = true; //while im here, mutex is active
xmlhttp = GetXmlHttpObject();
if (xmlhttp==null)
{
alert ("Votre navigateur ne supporte pas le XMLHTTP!");
return;
}
url = './consulter_ajax.php?id_domaine=' + id_domaine + '&sid=' + Math.random();
xmlhttp.onreadystatechange=doPopulerListeProgrammes;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}

function doPopulerListeProgrammes(){
if (xmlhttp.readyState==4)
{
document.getElementById("slcProgrammeIE").innerHTML=xmlhttp.responseText;
clearCours();
toggleElementDisable(document.getElementById("niveauADesactiver"), true); //désactive les niveaux
mutex = false; //release the mutex
}
}


And here comes js2.js



function getValeursDepartURL(){
//domaine
if(document.getElementById('slcDomaine')){
var domaine = document.getElementById('slcDomaine');
var id_domaine = getValeurURL('slcDomaine');
if(id_domaine){
selectIndexParValeur(domaine, id_domaine);
populerListeProgrammes(id_domaine);
}
}

while(mutex){} //INFINITE LOOP
//alert(mutex); //= true
//alert(mutex); //= false...

//Programme
if(document.getElementById('slcProgramme')){
var programme = document.getElementById('slcProgramme');
var id_programme = getValeurURL('slcProgramme');
if(id_programme){
selectIndexParValeur(programme, id_programme);
populerListeCours();
}
}

Jesdisciple
06-20-2009, 06:20 PM
while(mutex){} //INFINITE LOOPIf mutex is true, of course this is an infinite loop... The condition never changes; you're essentially saying while(true), which can work if you break from within it but you don't.

I can't say any more than that without working with the page. There are so many undefined functions there that I have no idea what the execution path is. Plus a working program is the best way to debug any code.