...

View Full Version : setInterval() Help



tecmeister
07-23-2010, 11:59 PM
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?




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.

Old Pedant
07-24-2010, 12:46 AM
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:


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";
}
}

tecmeister
07-24-2010, 08:40 AM
Thank you so much Old Pedant for the code you have given me.


Thanks,

tecmeister.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum