473,396 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

timedelays

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[i]; //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)

Jun 6 '06 #1
2 1258
Ivo
"Noggon" <li*******@gmail.com> 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[i]; //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/
Jun 6 '06 #2

Ivo wrote:
"Noggon" <li*******@gmail.com> 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[i]; //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! :-)

Jun 7 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.