problem with js recursive function

Discussion in 'JavaScript' started by Deathmaster, Aug 17, 2007.

  1. #1
    Hi,

    i have a problem with a recursive function.
    I'm working on a php-ajax application, and i managed to create a div that pops-up and displays the message from DB server, and I wanted this box to gradually disappear after a few seconds, a bit like outlook does when you receive a mail..

    my problem is that, if i declare the id of the layer everything works fine, but if i try and build a "generic" function - that is, passing the div-id into the function - it stops working
    I'm not a wizard in JS at all, so maybe some of you have a better idea of how to do this?
    here's the code and thank you for your help

    var n = 100;
    function opTimer() {
            /* id of the div is "updateMonitor" */
    	var obj = document.getElementById("updateMonitor");
    	if(n > 0){
    		obj.style.display="";
    		try {
                    /* Mozilla */
    		obj.style.MozOpacity = n/100;
    		} catch(e) {
                    /* IE */
    		obj.style.filters.alpha.opacity = n;
    		}
    		setTimeout("opTimer()",40);
    		n--;
    	} else {
    		obj.style.display="none"
    		try {
    		obj.style.MozOpacity = 1;
    		} catch (e) {
    		obj.style.filters.alpha.opacity=100;
    		}
    		n=100;
    		
    	}
    }
    Code (markup):
    in other words, the above works, this doesn't:
    var n = 100;
    function opTimer(divID) {
    	var obj = document.getElementById("updateMonitor");
    	if(n > 0){
    		obj.style.display="";
    		try {
    		obj.style.MozOpacity = n/100;
    		} catch(e) {
    		obj.style.filters.alpha.opacity = n;
    		}
    		setTimeout("opTimer(divID)",40);
    		n--;
    	} else {
    		obj.style.display="none"
    		try {
    		obj.style.MozOpacity = 1;
    		} catch (e) {
    		obj.style.filters.alpha.opacity=100;
    		}
    		n=100;
    		
    	}
    }
    Code (markup):
     
    Deathmaster, Aug 17, 2007 IP
  2. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #2
    You are not using divID...
    Try:
    var obj = document.getElementById(divID);
    Code (markup):
     
    krt, Aug 17, 2007 IP
  3. Deathmaster

    Deathmaster Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    sorry krt, that was a typo, i did mean to declare obj that way, and it still doesn't work...
    so:
    var n = 100;
    function opTimer(divID) {
    	var obj = document.getElementById(divID);
    	if(n > 0){
    		obj.style.display="";
    		try {
    		obj.style.MozOpacity = n/100;
    		} catch(e) {
    		obj.style.filters.alpha.opacity = n;
    		}
    		setTimeout("opTimer(divID)",40);
    		n--;
    	} else {
    		obj.style.display="none"
    		try {
    		obj.style.MozOpacity = 1;
    		} catch (e) {
    		obj.style.filters.alpha.opacity=100;
    		}
    		n=100;
    		
    	}
    }
    Code (markup):
    this one doesn't work, and i get no error message in the console or anything..
     
    Deathmaster, Aug 17, 2007 IP
  4. Deathmaster

    Deathmaster Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    oh well,
    I solved the problem, in case anyone is interested, it works like this
    var n = 100;
    function opTimer(divID) {
    	var obj = document.getElementById(divID);
    	if(n > 0){
    		obj.style.display="";
    		try {
    		obj.style.MozOpacity = n/100;
    		} catch(e) {
    		obj.style.filters.alpha.opacity = n;
    		}
    		setTimeout("opTimer('" + divID + "')",40);
    		n--;
    	} else {
    		obj.style.display="none"
    		try {
    		obj.style.MozOpacity = 1;
    		} catch (e) {
    		obj.style.filters.alpha.opacity=100;
    		}
    		n=100;
    		
    	}
    }
    Code (markup):
    cheers
     
    Deathmaster, Aug 21, 2007 IP