PDA

View Full Version : Race condition :S


Jahren
06-17-2009, 09:37 PM
hi guys

I need help solving a rather complicated matter xD

I'm building an advanced search engine

http://i59.photobucket.com/albums/g311/maelia123/label.jpg

HTML includes 2 javascript files. lets say js1 and js2

js1 includes Ajax methods to write <select> with its options from the database.
The next <select> depends of the selected id from the one before.

this parts works great!
I populate the first select. onchange, I populate the second one. when the second one changes, I fill the third select.

so far so good.
js2 has an other job :
when the page finishes loading, i'm calling a method that takes the URL string and apply the submitted value to the form inputs.

I can check for the value of the first select box and apply it.
I fire what onchange() event does with populerListeProgrammes(id_domaine);

with its id, it should fill the second select EXCEPT, it doesn't exist yet!

Race condition. Ajax hasn't finished getting the values and the other javascript function tried to find the second select..
I don't want to set a timer to way a fixed amount of time, it isn't cute :P

I'm sorry for that bad english, ask a question and i'll clarify if needed :P



function getValeursDepartURL(){
//domaine
if(document.getElementById('slcDomaine')){
var domaine = document.getElementById('slcDomaine');
var id_domaine = getValeurURL('slcDomaine');
selectIndexParValeur(domaine, id_domaine);
populerListeProgrammes(id_domaine);
}
setTimeout(function(){}, 5000);
alert('test');
//Programme
if(document.getElementById('slcProgramme')){
alert('tset');
var programme = document.getElementById('slcProgramme');
var id_programme = getValeurURL('slcProgramme');
selectIndexParValeur(programme, id_programme);
populerListeCours();
}

var auteur = document.getElementById('strAuteur');
auteur.value = getValeurURL('strAuteur');
}

function selectIndexParValeur(obj, valeur){
for(var i = 0; i < obj.options.length; ++i){
if(obj.options[i].value == valeur){
obj.selectedIndex = i;
}
}
}



I tried using a mutex variable.
declare a global var mutex = false in js1
whenever Ajax function is called, it's set to true until it's finished.

so in js2, I wanted to wait until mutex isnt true

but I can't seem to access js1 global var from js2.. so I get infinite loop.
Why can I call js1 functions and not check js1 vars?

edit :
My mutex WILL change to false if I set an alert before the call.
but while(mutex) will loop forever..
what am I missing?