timedelays

N

Noggon

Hi

I know how to use setTimeout, but I'm having a problem with my script
in that I don't really want a function called by the timeout. So I need
a way to insert a brief pause. (I know that theres no direct facility
to do so in javascript, and I don't want to waste cpu time by using a
date and time to do this.)

The situation is that I have an array full of hex codes to change
colors of an div, however I need a delay in order to make it look
right.

A loop adjusts the codes for me,

function fruitLoop()
{
for(i=0;i<=213;i++)
{
//would like a pause here
ook.color = myarray; //adjusts div to another color
}

}

So how do I insert a pause? It should be simple, but I just can't think
of a suitable mechanism.

Any ideas appreciated.

(btw: I read the faq on this, and group posts futher back, but couldn't
find the situation like the one I seem to have, all of them seemed to
be happy with a function call)
 
I

Ivo

Noggon said:
Hi
Hi!

I know how to use setTimeout, but I'm having a problem with my script
in that I don't really want a function called by the timeout. ...

What 's against using a function? Seems a reasonable way to go, though there
are other ways.
The situation is that I have an array full of hex codes to change
colors of an div, however I need a delay in order to make it look
right.

A loop adjusts the codes for me,

function fruitLoop()
{
for(i=0;i<=213;i++)
{
//would like a pause here
ook.color = myarray; //adjusts div to another color
}

}

It looks like "ook" is a global variable, and is referring to the div. And
"myarray" is global, too, right? Then put this code inside the loop:

window.setTimeout( 'ook.color = myarray[' + i + '];', i * 200 );

and "ook" will change colors at a rate of five per second. Make the 200 a
1000 and you have one color per second.

hth
ivo
http://4umi.com/web/javascript/
 
N

Noggon

Ivo said:
Noggon said:
Hi
Hi!

I know how to use setTimeout, but I'm having a problem with my script
in that I don't really want a function called by the timeout. ...

What 's against using a function? Seems a reasonable way to go, though there
are other ways.
The situation is that I have an array full of hex codes to change
colors of an div, however I need a delay in order to make it look
right.

A loop adjusts the codes for me,

function fruitLoop()
{
for(i=0;i<=213;i++)
{
//would like a pause here
ook.color = myarray; //adjusts div to another color
}

}

It looks like "ook" is a global variable, and is referring to the div. And
"myarray" is global, too, right? Then put this code inside the loop:

window.setTimeout( 'ook.color = myarray[' + i + '];', i * 200 );

and "ook" will change colors at a rate of five per second. Make the 200 a
1000 and you have one color per second.

hth


It does, thanks very much, thats fantasic! :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,266
Latest member
DavidaAlla

Latest Threads

Top