Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Multiple setTimeout in function?

Reply
Thread Tools

Multiple setTimeout in function?

 
 
Max
Guest
Posts: n/a
 
      10-10-2006
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

 
Reply With Quote
 
 
 
 
knizacky@gmail.com
Guest
Posts: n/a
 
      10-10-2006

Max wrote:
> 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

 
Reply With Quote
 
 
 
 
Evertjan.
Guest
Posts: n/a
 
      10-10-2006
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.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
 
Reply With Quote
 
Max
Guest
Posts: n/a
 
      10-10-2006


On Oct 10, 11:03 am, "Evertjan." <(E-Mail Removed)> wrote:
> 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

 
Reply With Quote
 
Robin
Guest
Posts: n/a
 
      10-11-2006
Max wrote:
> document.getElementById('mydiv').style.length='10p x'


Err, what's 'length'? I don't see this property at:
http://www.w3schools.com/css/css_reference.asp

Shouldn't it be 'width' or 'height', or am I missing something?

Robin
 
Reply With Quote
 
Dr John Stockton
Guest
Posts: n/a
 
      10-11-2006
JRS: In article <(E-Mail Removed). com>,
dated Tue, 10 Oct 2006 08:47:52 remote, seen in
news:comp.lang.javascript, Max <(E-Mail Removed)> posted :
>
>Anybody know a simple solution for this?


FAQ 4.20.

It's a good idea to read the newsgroup and its FAQ. See below.
--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
 
Reply With Quote
 
Dr John Stockton
Guest
Posts: n/a
 
      10-11-2006
JRS: In article <egibhj$4bf$(E-Mail Removed)>, dated Wed, 11 Oct
2006 09:55:15 remote, seen in news:comp.lang.javascript, Robin
<(E-Mail Removed)> posted :
>Max wrote:
>> document.getElementById('mydiv').style.length='10p x'

>
>Err, what's 'length'? I don't see this property at:
>http://www.w3schools.com/css/css_reference.asp


Not the most reliable of sites, IIRC, but it should be OK here.

>Shouldn't it be 'width' or 'height', or am I missing something?


I'd suggest 'fontSize'. But specifying absolute font-sizes is generally
deprecated.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
 
Reply With Quote
 
 
 
Reply

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
javascript setTimeout does not work =?Utf-8?B?RQ==?= ASP .Net 4 05-02-2006 08:21 PM
setTimeout JR HTML 3 04-15-2004 12:07 PM
settimeout j van c HTML 5 12-07-2003 09:08 AM
socket settimeout ? Colin Brown Python 0 12-03-2003 12:28 AM
Smart navigation and js setTimeout Andy Pickering ASP .Net 1 10-27-2003 10:51 AM



Advertisments