Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    Regular Coder
    Join Date
    May 2009
    Thanked 1 Time in 1 Post

    Race condition :S

    hi guys

    I need help solving a rather complicated matter xD

    I'm building an advanced search engine

    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(){
    		var domaine = document.getElementById('slcDomaine');
    		var id_domaine = getValeurURL('slcDomaine');
    		selectIndexParValeur(domaine, id_domaine);
    	setTimeout(function(){}, 5000);
    		var programme = document.getElementById('slcProgramme');
    		var id_programme = getValeurURL('slcProgramme');
    		selectIndexParValeur(programme, id_programme);
    	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?
    Last edited by Jahren; 06-17-2009 at 09:21 PM.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts