Results 1 to 2 of 2

Thread: Racing condition help

  1. #1
    Join Date
    Jun 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Racing condition help

    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 :
    Code:
    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

    Code:
    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();
    		}
    	}

  2. #2
    Join Date
    Jul 2006
    Posts
    497
    Thanks
    8
    Thanked 70 Times in 70 Posts

    Default

    Code:
    while(mutex){} //INFINITE LOOP
    If 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.
    -- Chris
    informal JavaScript student of Douglas Crockford
    I like wikis - a lot.

Bookmarks

Posting Permissions

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