Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-23-2010, 10:59 PM   PM User | #1
tecmeister
New to the CF scene

 
Join Date: Aug 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
tecmeister is an unknown quantity at this point
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.
tecmeister is offline   Reply With Quote
Old 07-23-2010, 11:46 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,187
Thanks: 59
Thanked 3,995 Times in 3,964 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 07-24-2010, 07:40 AM   PM User | #3
tecmeister
New to the CF scene

 
Join Date: Aug 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
tecmeister is an unknown quantity at this point
Thank you so much Old Pedant for the code you have given me.


Thanks,

tecmeister.
tecmeister is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:59 PM.


Advertisement
Log in to turn off these ads.