Multiple setTimeout in function?

M

Max

Hi,

I'm not exactly sure why this doesn't work. I'm basically just trying a
simple approach at a slide down div.

function slide_div {


setTimeout("document.getElementById('mydiv').style.length='10px';",1000);

setTimeout("document.getElementById('mydiv').style.length='20px';",1000);

setTimeout("document.getElementById('mydiv').style.length='30px';",1000);

setTimeout("document.getElementById('mydiv').style.length='40px';",1000);

setTimeout("document.getElementById('mydiv').style.length='50px';",1000);

setTimeout("document.getElementById('mydiv').style.length='60px';",1000);

}

It seems like it just runs the last setTimeout line and pops out all at
once. I've even tried adding a=, b=, c=,. etc at the begining of each
line to no avail.

Anybody know a simple solution for this?

Thanks,
Max
 
K

knizacky

Max said:
Hi,

I'm not exactly sure why this doesn't work. I'm basically just trying a
simple approach at a slide down div.

function slide_div {


setTimeout("document.getElementById('mydiv').style.length='10px';",1000);

setTimeout("document.getElementById('mydiv').style.length='20px';",1000);

setTimeout("document.getElementById('mydiv').style.length='30px';",1000);

setTimeout("document.getElementById('mydiv').style.length='40px';",1000);

setTimeout("document.getElementById('mydiv').style.length='50px';",1000);

setTimeout("document.getElementById('mydiv').style.length='60px';",1000);

}

It seems like it just runs the last setTimeout line and pops out all at
once. I've even tried adding a=, b=, c=,. etc at the begining of each
line to no avail.

Anybody know a simple solution for this?

Thanks,
Max

Increment your milliseconds instead of using 1000 for all of them. e.g.
1000,2000,etc
 
E

Evertjan.

Max wrote on 10 okt 2006 in comp.lang.javascript:
I'm not exactly sure why this doesn't work. I'm basically just trying a
simple approach at a slide down div.

function slide_div {

a function needs ()
setTimeout("document.getElementById ('mydiv').style.length='10px';",1000);
('mydiv').style.length='20px';",1000);
('mydiv').style.length='30px';",1000);
('mydiv').style.length='40px';",1000);
('mydiv').style.length='50px';",1000);
('mydiv').style.length='60px';",1000);

}

It seems like it just runs the last setTimeout line and pops out all at
once. I've even tried adding a=, b=, c=,. etc at the begining of each
line to no avail.


They start all at [nearly] the same time,
so they all fire at [nearly] the same second later.
setTimeout() is NOT a wait() function.

Try:

==================================
var mydiv = document.getElementById('mydiv');
var myLength = 0;
slide_div();

function slide_div() {
mydiv.style.length= myLength + 'px';
myLength += 10;
if (myLength<=60)
setTimeout("slide_div()",1000);
};
===================================

not tested.
 
M

Max

Max wrote on 10 okt 2006 in comp.lang.javascript:
I'm not exactly sure why this doesn't work. I'm basically just trying a
simple approach at a slide down div.
function slide_div {a function needs ()
setTimeout("document.getElementById('mydiv').style.length='10px';",1000);
setTimeout("document.getElementById('mydiv').style.length='20px';",1000);
setTimeout("document.getElementById('mydiv').style.length='30px';",1000);
setTimeout("document.getElementById('mydiv').style.length='40px';",1000);
setTimeout("document.getElementById('mydiv').style.length='50px';",1000);
setTimeout("document.getElementById('mydiv').style.length='60px';",1000);



It seems like it just runs the last setTimeout line and pops out all at
once. I've even tried adding a=, b=, c=,. etc at the begining of each
line to no avail.They start all at [nearly] the same time,
so they all fire at [nearly] the same second later.
setTimeout() is NOT a wait() function.

Try:

==================================
var mydiv = document.getElementById('mydiv');
var myLength = 0;
slide_div();

function slide_div() {
mydiv.style.length= myLength + 'px';
myLength += 10;
if (myLength<=60)
setTimeout("slide_div()",1000);};===================================

not tested.

Thanks for the suggestions. This worked fine and I now understand what
I was doing wrong.

Max
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top