Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > setTimeout only delays first time in loop

Reply
Thread Tools

setTimeout only delays first time in loop

 
 
barry@polisource.com
Guest
Posts: n/a
 
      09-13-2006
I'm having a problem creating a delay before each list item gets moved
( http://www.polisource.com/PublicMisc...meout-bug.html
). Choose any number of columns, vertical layout, and view as a web
page. They'll be a small delay before the entire bunch of list items
converts from left-to-right to top-to-bottom order. I expected a delay
before each item is moved. It looks like I have to flush the buffer to
get setTimeout to work, but I don't think there's a built-in flush
function in Javascript anymore. Or else setTimeout only delays the
function it calls the first time it's called and subsequently calls the
function with no delay. In this script I have:

setTimeout("funtest(" + li_count + "," + pos_change_left + "," +
pos_change_top + ")",700);

which calls:

function funtest(aaa,bbb,ccc)
{
var li = $('mylist').getElementsByTagName('LI');
li[aaa].style.position = 'relative';
li[aaa].style.left = bbb + 'px';
li[aaa].style.top = ccc + 'px';
}

once for each li that's moved (20 of them), but there's only one delay,
then they're all moved. If I put an alert in the function, it's
triggered 20 times, once before every change in position of a li, so
why aren't there 20 delays (one before each move) instead of just one?

 
Reply With Quote
 
 
 
 
Erwin Moller
Guest
Posts: n/a
 
      09-13-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> I'm having a problem creating a delay before each list item gets moved
> ( http://www.polisource.com/PublicMisc...meout-bug.html
> ). Choose any number of columns, vertical layout, and view as a web
> page. They'll be a small delay before the entire bunch of list items
> converts from left-to-right to top-to-bottom order. I expected a delay
> before each item is moved. It looks like I have to flush the buffer to
> get setTimeout to work, but I don't think there's a built-in flush
> function in Javascript anymore. Or else setTimeout only delays the
> function it calls the first time it's called and subsequently calls the
> function with no delay. In this script I have:
>
> setTimeout("funtest(" + li_count + "," + pos_change_left + "," +
> pos_change_top + ")",700);
>
> which calls:
>
> function funtest(aaa,bbb,ccc)
> {
> var li = $('mylist').getElementsByTagName('LI');
> li[aaa].style.position = 'relative';
> li[aaa].style.left = bbb + 'px';
> li[aaa].style.top = ccc + 'px';
> }
>
> once for each li that's moved (20 of them), but there's only one delay,
> then they're all moved. If I put an alert in the function, it's
> triggered 20 times, once before every change in position of a li, so
> why aren't there 20 delays (one before each move) instead of just one?


Hi,

Not my area of expertise, but I think I heard somebody in here say before
that all stylesheet-updates are executed when the JS-function returns.
Has something to do with 1 thread for javascript, but I am not sure.

It has nothing to do with 'flushing a buffer'. Which buffer?

And if you want them to move one after each other, give the next setTimeOut
from within the excuting function (funtest in this case).
Or make sure all setTimeout calls have an increasing interval.
Like this:
setTimeout("funtest('bla','bla','bluh')",700);
setTimeout("funtest('bla','bla2','bluh')",1400);
etc.

I cannot see by your code where and how the setTimeOut is implemented right
now.

Regards,
Erwin Moller
 
Reply With Quote
 
 
 
 
barry@polisource.com
Guest
Posts: n/a
 
      09-13-2006
Thanks guys. I incremented the delay for each iteration and it worked.

Jim Davis wrote:

> The key is going to be how you're calling that timeout. My guess is you've
> put it in a loop and set 20 timeouts - think about it: of course they'd all
> move at the same time (or very close) since you're setting them all at once.
> ;^)


I'm not sure I understand how the script is executed for it to work the
way it does. The way I figured, my original timeout said "delay 700ms,
then execute the named function (funtest)," then after funtest is
executed (which changes the position of a single li element), the
statement after the timeout is executed, which is within the same loop
of the timeout, and the loop would cause the timeout to be executed
again, causing another 700ms delay before another li changes position.

But as far as I could tell, you're saying that Javascript sees the
delay the first time, delays 700ms, then thinks "ok, I delayed 700ms
before funtest, and even if I encounter the 700ms timeout 19 more
times, that job was done so I don't have to delay again, but I still
have to execute funtest each time."

Anyone know of any online documentation for setTimeout that makes it
clear that the latter is the way it works rather than the former, or is
there a problem with my logic?

 
Reply With Quote
 
barry@polisource.com
Guest
Posts: n/a
 
      09-14-2006
I received a response from TheBearMay at
http://www.webdeveloper.com/forum/sh...d.php?p=637609 that
clarified things for me. "SetTimeout is not a thread delay, but a
function to run a command after a delay; i.e. it doesn't stop thread
execution, but spawns another thread on a time delay." That explains
the behavior. I hadn't read that anywhere. I guess someone learning
Javascript should buy a book. Some important information is missing
from the online documentation I've seen.

 
Reply With Quote
 
Dr John Stockton
Guest
Posts: n/a
 
      09-14-2006
JRS: In article <(E-Mail Removed) .com>,
dated Wed, 13 Sep 2006 10:29:22 remote, seen in
news:comp.lang.javascript, (E-Mail Removed) posted :

>But as far as I could tell, you're saying that Javascript sees the
>delay the first time, delays 700ms, then thinks "ok, I delayed 700ms
>before funtest, and even if I encounter the 700ms timeout 19 more
>times, that job was done so I don't have to delay again, but I still
>have to execute funtest each time."
>
>Anyone know of any online documentation for setTimeout that makes it
>clear that the latter is the way it works rather than the former, or is
>there a problem with my logic?


The latter.

All sound Javascript references, except the most condensed, indicate
that setTimeout causes the given code to be started after (at least) the
given delay, and do not say that the current thread of execution stops
for that delay.

Example :
setTimeout Evaluates an expression or calls a function once after a
specified number of milliseconds has elapsed.
...
setTimeout does not stall the script. The script
continues immediately (not waiting for the timeout to
expire). The call simply schedules a future event.


Newsgroup FAQ 4.20 is also clear enough.

The FAQ is cited daily in the newsgroup, and you should have read it
before posting.

It's a good idea to read the newsgroup and its FAQ.
--
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
 
barry@polisource.com
Guest
Posts: n/a
 
      09-18-2006
Dr John Stockton wrote:
> The FAQ is cited daily in the newsgroup, and you should have read it
> before posting.
>
> It's a good idea to read the newsgroup and its FAQ.


I access this group through Google Groups and didn't see anything about
a FAQ, though I didn't read any posts. Just now I looked at the second
page of post titles and I saw " FAQ Topic - How do I modify the current
browser window?" I wouldn't have clicked that if I saw it before. When
I post to a newsgroup, I just check whether there's too much spam
before I post, and I always do my own research first if I think there's
any chance of finding the answer. I read several online Javascript
references and saw nothing like what you quoted that mentioned
threading.

Can't the Javascript developers just make a regular sleep function that
doesn't thread?

 
Reply With Quote
 
Dr John Stockton
Guest
Posts: n/a
 
      09-19-2006
JRS: In article <(E-Mail Removed). com>,
dated Mon, 18 Sep 2006 10:09:17 remote, seen in
news:comp.lang.javascript, (E-Mail Removed) posted :
>Dr John Stockton wrote:
>> The FAQ is cited daily in the newsgroup, and you should have read it
>> before posting.
>>
>> It's a good idea to read the newsgroup and its FAQ.

>
>I access this group through Google Groups and didn't see anything about
>a FAQ, though I didn't read any posts.


You are naive and rude. A normal person does not, in outside life,
contribute to a discussion without listening for a while first. Looking
at Subjects is inadequate.

> Just now I looked at the second
>page of post titles and I saw " FAQ Topic - How do I modify the current
>browser window?" I wouldn't have clicked that if I saw it before. When
>I post to a newsgroup, I just check whether there's too much spam
>before I post, and I always do my own research first if I think there's
>any chance of finding the answer.


Your research must have been sadly limited if you have not come across
the concept of "FAQ" before - perhaps this is your first venture into
technical newsgroups.

> I read several online Javascript
>references and saw nothing like what you quoted that mentioned
>threading.


Perhaps you do not understand the distinction between an authoritative
reference and an amateurish effort?

>Can't the Javascript developers just make a regular sleep function that
>doesn't thread?


Of course they can. But it would be entirely unnecessary. If you want
an efficient delay, just call setTimeout when the present thread is
finished, and give it the new code; adapt your mode of thinking to the
environment.

If you want an inefficient machine-hogging one, just write a trivial
wait-loop using new Date(), such as
for (D = +new Date()+3000 ; +new Date() < D ; ) ; // Delay(3000)
but don't be so silly as to put it on a Web page.

It's a good idea to read the newsgroup and its FAQ.
--
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
 
barry@polisource.com
Guest
Posts: n/a
 
      09-20-2006
Dr John Stockton wrote:
> You are naive and rude. A normal person does not, in outside life,
> contribute to a discussion without listening for a while first. Looking
> at Subjects is inadequate.


You're the one who's naive and rude. In addition, despite your
expertise, this newsgroup would be better off without you. I appreciate
the help of someone knowledgeable just like everyone else, but not if
it means allowing someone as rude as you to get an ego boost from it
and help publicize his website in his signature. I'd rather put in the
extra effort to find a solution from another source or another member
of the newsgroup than to have myself and others have to deal with
people with sticks up their ass like you. Nobody "in outside life" who
makes comments like yours should expect to get far in a civilized
society unless they have a lot of money. If you don't change your
manner, you better hope that whatever clique is putting up with your
crap never goes away.


Dr John Stockton wrote:
> Your research must have been sadly limited if you have not come across
> the concept of "FAQ" before - perhaps this is your first venture into
> technical newsgroups.

....
> Perhaps you do not understand the distinction between an authoritative
> reference and an amateurish effort?


My research included many carefully formed search engine queries and
visits to popular Javascript webpages that I've bookmarked over the
years. In a prior post to this thread (
http://groups.google.com/group/comp....2346180439e99e
) I said "I received a response from TheBearMay at
http://www.webdeveloper.com/forum/sh...d.php?p=637609 that
clarified things for me." If you read the response, you'll see that
TheBearMay (a Super Moderator) informed me about the threads used in
setTimeout and recommended three webpages for additional reading on the
topic, some of which I'd previously seen. I responded to him saying
"none of the links you provided or any of the other pages I've read say
anything like that." With all the research I did and all the pages
specifically on setTimeout that I read, I'm right to complain about the
lack of decent online documentation for Javascript. I had noticed this
for years.


Dr John Stockton wrote:
> Your research must have been sadly limited if you have not come across
> the concept of "FAQ" before - perhaps this is your first venture into
> technical newsgroups.


Perhaps you should stop assuming things that you have no way of
knowing, especially when it takes the form of an insult. The reason I
wouldn't have clicked "FAQ Topic - How do I modify the current browser
window?" if I had looked that far down the subject titles is...and I
thought this was obvious...because the title clearly has nothing to do
with my question. As I said, "I access this group through Google Groups
and didn't see anything about
a FAQ."

The subject of this newsgroup and of the posts makes it obvious that my
question was appropriate, especially after the effort I took to find
the answer. I guess you feel I should have concentrated that effort on
this newsgroup. It's true that in that case I might have come across
your signature with the link to your FAQ in which the answer I sought
is buried. I hope it doesn't offend you that I searched a wider data
set than that. No, actually I hope you are offended.


> >Can't the Javascript developers just make a regular sleep function that
> >doesn't thread?

>
> Of course they can. But it would be entirely unnecessary. If you want
> an efficient delay, just call setTimeout when the present thread is
> finished, and give it the new code; adapt your mode of thinking to the
> environment.


As someone mentioned earlier in this thread, "The second option (and,
if I'm correct that you've place the timeout in a
loop then probably the easier one for you) is to stagger the timeouts."
Yes, much easier. I wrote my script in the most intuitive way for me,
and I would have appreciated a plain old delay with no threading and
not having to figure out how to adapt my code. Increasing the delay by
the amount of the original delay for each iteration isn't such an
obvious solution even if you know about the threading. KISS.


> If you want an inefficient machine-hogging one, just write a trivial
> wait-loop using new Date(), such as
> for (D = +new Date()+3000 ; +new Date() < D ; ) ; // Delay(3000)


No thanks.


> It's a good idea to read the newsgroup and its FAQ.


You never told me where this FAQ exists. You mean the link in your
signature?

Feel free to consider all question rhetorical and not to respond.

 
Reply With Quote
 
Dr John Stockton
Guest
Posts: n/a
 
      09-20-2006
JRS: In article <(E-Mail Removed). com>,
dated Tue, 19 Sep 2006 17:14:10 remote, seen in
news:comp.lang.javascript, (E-Mail Removed) posted :

> ...
>and help publicize his website in his signature.
> ...


And why not? IMHO, the site is useful only if people read it, and for
that they need to know that it exists. But perhaps you think that I get
financial gain from people reading it? If so, you have a very sordidly
mercenary attitude.

>> It's a good idea to read the newsgroup and its FAQ.

>
>You never told me where this FAQ exists. You mean the link in your
>signature?


I require, but fortunately do not presume, a modicum of intelligence in
my readers.

You are a windbag.

It's a good idea to read the newsgroup and its FAQ.
--
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
 
Lee
Guest
Posts: n/a
 
      09-21-2006
(E-Mail Removed) said:
>
>As I said, "I access this group through Google Groups
>and didn't see anything about
>a FAQ."


What does that mean? Does Google Group normally identify a FAQ
for newsgroups? If so, it seems to be unreliable. As was pointed
out, you should always scan the past week's titles in the newsgroup
for any reference to a FAQ and follow it, even if the particular
reference doesn't seem to refer to your particular problem.


>> >Can't the Javascript developers just make a regular sleep function that
>> >doesn't thread?

>>
>> Of course they can. But it would be entirely unnecessary. If you want
>> an efficient delay, just call setTimeout when the present thread is
>> finished, and give it the new code; adapt your mode of thinking to the
>> environment.

>
>As someone mentioned earlier in this thread, "The second option (and,
>if I'm correct that you've place the timeout in a
>loop then probably the easier one for you) is to stagger the timeouts."
>Yes, much easier. I wrote my script in the most intuitive way for me,
>and I would have appreciated a plain old delay with no threading and
>not having to figure out how to adapt my code. Increasing the delay by
>the amount of the original delay for each iteration isn't such an
>obvious solution even if you know about the threading. KISS.


That's probably not the simplest or most supportable way to use
setTimeout() to do what you want to do.
See http://www.jibbering.com/faq/#FAQ4_20

Web browsers are event-driven systems. Event-driven systems should
never sleep. Providing setTimeout() and setInterval() instead of
any sort of sleep() was a well-considered design decision.


--

 
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
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Delays-delays-delays Colin Palmer Computer Support 5 02-26-2008 04:41 AM
Probelm to post XML data in a loop. First time XML is posted, second time data is getting truncated. Please help. vamsi.aluru@gmail.com Perl Misc 7 02-14-2006 12:09 PM
Thread time - strange delays FrankEsser C++ 3 07-04-2005 01:17 PM
delays: inertial delays vs. transport delays Neil Zanella VHDL 2 10-20-2003 07:27 AM



Advertisments