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
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    setInterval() Help

    Hi Guys,

    I'm new to javascript and I'm trying to have a delay. I seem to be having trouble with the setInterval(). Will someone please be able to tell me what have I have done wrong?

    Code:
    function viewData()
    {
    	var num = 10;
    	setLoop(num);
    }
    
    function setLoop(i)
    {
    	setInterval(loopOpacity(i), 300);
    }
    
    function loopOpacity(i)
    {
    	i--;
    	var iNum = "0." + i;
    	document.getElementById("fade").style.opacity = iNum;
    	
    	if(i == 4)
    	{
    		document.getElementById("viewData").className = "view";
    		clearInterval(int);
    	}
    	else
    		setLoop(i);
    }
    
    function closeData()
    {
    	document.getElementById("fade").style.opacity = 1;;
    	document.getElementById("viewData").className = "close";	
    }
    Thanks for your help,

    tecmeister.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,965
    Thanks
    79
    Thanked 4,429 Times in 4,394 Posts
    The way you are using this code, you should *NOT* be using setInterval.

    Use setTimeout( ) instead.

    Also, you need to pass the argument to setTimeout as a string.

    You also will *NOT* need or want the clearInterval() call, at all.

    Oh, w.t.h.

    Here, let's simplify for you:
    Code:
    function viewData()
    {
        loopOpacity(100);
    }
    
    function loopOpacity(percent)
    {
        document.getElementById("fade").style.opacity = (percent/100);
        if( percent > 40 )
        {
            setTimeout("loopOpacity(" + (percent-10) + ")", 300);
        } else {
            document.getElementById("viewData").className = "view";
        }
    }
    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.

  • #3
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you so much Old Pedant for the code you have given me.


    Thanks,

    tecmeister.


  •  

    Posting Permissions

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