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

    Loop not stopping :O :(

    Code:
    // Values for fading
    var fadeBy = 10; //Amount to fade by
    var fadeTime = 120; //Time in milliseconds to fade. 
    var ie_counter = 0; //Counter for crappy IE.
    var fade_to = 70;
    function fader(obj) {
        var div = document.getElementById(obj);
    	
        //Object details
        var opacity = div.style.opacity * 100 + fadeBy; //Fade (FF)
        ie_counter = ie_counter + fadeBy;
        var opacity_ie = div.style.filter = "alpha(opacity=" + ie_counter + ")"; //Fade (IE)
        div.style.opacity = opacity / 100;
    	if ( ie_counter != fade_to || opacity != fade_to ) {
        	setTimeout( "fader('" + obj + "')", fadeTime );
    	}
    }
    The loop stops for firefox (good browser), but not for internet explorer for some reason :/

    Apart for the fact that IE is crap why is this?

  • #2
    New Coder
    Join Date
    Jul 2010
    Posts
    61
    Thanks
    0
    Thanked 21 Times in 21 Posts
    Code:
    if ( ie_counter != fade_to || opacity != fade_to )
    To stop, both those conditions have to evaluate false. All you have to do is determine which one doesn't.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,554
    Thanks
    80
    Thanked 4,620 Times in 4,583 Posts
    Because opacity != fade_to will *ALWAYS* be TRUE when using IE!!!! opacity will be undefined!

    So how CAN it stop???

    You are working too hard. Keep it simple:
    Code:
    var fadeBy = 10; //Amount to fade by
    var fadeTime = 120; //Time in milliseconds to fade. 
    var fade_counter = 0; var fade_to = 70;
    function fader(obj) {
        var div = document.getElementById(obj);
    	
        fade_counter += fadeBy;
        div.style.filter = "alpha(opacity=" + fade_counter + ")"; //Fade (IE)
        div.style.opacity = opacity / 100;
        if ( fade_counter < fade_to ) {
        	setTimeout( "fader('" + obj + "')", fadeTime );
    	}
    }
    You should use fade_counter < fade_to as the test, in case you have values such as fadeBy = 7 and fade_to = 80!!
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    martynball (07-22-2010)


  •  

    Posting Permissions

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