SetTimeout question...

E

Erwin Moller

Thomas Allen schreef:
Thomas Allen schreef:


I'm trying to use SetTimeout to rotate some image sources, and I can't
get it to work. I know that there are other ways to pull this off, but
I'm curious as to what I need to change to get this script to work:
(function($) {
$(document).ready(function() {
var sponsors = $('.sponsors');
var sponsor_images = sponsors.find('img').remove();
var sponsor_frame = $('<img>').attr('src',
setTimeout(function() {
for(var i = 0; i < sponsor_images.length; i++) {
return $(sponsor_images).attr('src');
};
}, 3000)
);
sponsors.html(sponsor_frame);
});
}) (jQuery);
If it isn't clear, here's what should happen:
1. Find the sponsor block
2. Extract the images as a jQuery object, removing them
3. Create a new image node
4. Set this image's source to change every three seconds to the next
one from the extracted images object
I've verified that the only part of the script that isn't working
properly is the setTimeout call, so I must be doing something wrong.
In case it's at all useful, here's the accompanying markup:
<div class="sponsors">
<img src="/handa/images/sponsors/aecom.png" alt="AECOM" />
<img src="/handa/images/sponsors/cdm.png" alt="CDM" />
<img src="/handa/images/sponsors/pankow.png" alt="Charles Pankow
Foundation" />
<img src="/handa/images/sponsors/moretrench.png" alt="Moretrench" />
<img src="/handa/images/sponsors/figg.png" alt="Figg" />
<img src="/handa/images/sponsors/hntb.png" alt="HNTB" />
<img src="/handa/images/sponsors/kiewit.png" alt="Kiewit" />
<img src="/handa/images/sponsors/mcgraw.png" alt="McGraw Hill
Construction - ENR" />
<img src="/handa/images/sponsors/weidlinger.png" alt="Weidlinger
Associates Inc. - Consulting Engineers" />
</div>
And I greatly appreciate any help!
Thanks,
Thomas
Here's a simpler alternative:
(function($) {
$(document).ready(function() {
var sponsors = $('.sponsors');
var sponsor_images = sponsors.find('img').remove();
var sponsor_frame = $('<img>');
for(var i = 0; i < sponsor_images.length; i++) {
setTimeout("sponsor_frame.attr('src', $(sponsor_images).attr
('src'))", 3000);
};
sponsors.html(sponsor_frame);
});
}) (jQuery);
However, I don't understand scope inside of setTimeout: I get
"undefined" errors for all of the variables in the setTimeout command,
and I'm not too keen on making each one global.
Thomas

If you can rewrite your script in such a way you don't use $ and JQuery,
maybe it becomes clearer and debugable (for me).
If you MUST use JQuery, why don't you post to their forum?

Regards,
Erwin Moller

--
"There are two ways of constructing a software design: One way is to
make it so simple that there are obviously no deficiencies, and the
other way is to make it so complicated that there are no obvious
deficiencies. The first method is far more difficult."
-- C.A.R. Hoare


Hi Erwin,


Hi Thomas,

[Please don't qoute signatures. Any good newsreader will do that for you
automagically.]

I know, but I felt that this was a more JavaScript-oriented question.

I see others have pointed out already JQuery is not considered (good)
JavaScript by many developers. (But it is JavaScript of course.)
David Mark, the one you are having a not-so-friendly discussion with,
actually took the time to study JQuery (different versions) quite
thoroughly and he published his results in here earlier.
His findings, plus what other people I respect in comp.lang.javascript
think about JQuery, plus my own programming experience and natural
paranoid, gives me a very clear picture of JQuery: stay away from it.

Bottomline: I still cannot help you with your problem. Your simplified
version is still based on JQuery and I have no clue how JQuery's
internals work. :-/
Sorry I cannot be of help. Maybe try a JQuery forum I suggested earlier.

Good luck.

Regards,
Erwin Moller

PS: a little reflection if I may:
I can imagine you feel frustrated now. You have a problem, you post a
friendly message and ask for help.
Then people in here tells you to loose JQuery first, a lib you like,
before they can/will help.
But for your own sake: take a little distance, don't make it personal,
and look at the problem again: A lot of experienced JavaScript
developers advise you to avoid JQuery. Maybe this is actually good advise?
Why don't you roll your own full JavaScript instead? I always do that
and it gives me a lot of satidfaction when a difficult job is done right
by me in 'pure' JavaScript/DOM (no libs). Do you really need JQuery?
With a little study you can start doing all DOM manipulation yourself.
That way you will also be able to debug a lot better than now.


--
"There are two ways of constructing a software design: One way is to
make it so simple that there are obviously no deficiencies, and the
other way is to make it so complicated that there are no obvious
deficiencies. The first method is far more difficult."
-- C.A.R. Hoare
 
E

Erwin Moller

Tim Streater schreef:
[loadsa stuff snipped]

I would appreciate it if you guys could:

1) top post when you only have one line to add to billyuns of others.
2) learn to snip.

Your choice.

I strongly suggest the second. Topposting is a bad habbit IMHO. ;-)
It destroys the chronological order of the discussion.

Regards,
Erwin Moller

--
"There are two ways of constructing a software design: One way is to
make it so simple that there are obviously no deficiencies, and the
other way is to make it so complicated that there are no obvious
deficiencies. The first method is far more difficult."
-- C.A.R. Hoare
 
G

Gregor Kofler

Am Mon, 30 Mar 2009 11:22:36 -0700 schrieb Thomas Allen:

Dude, I was just asking for help with a specific JavaScript problem, not
a rant filled with douchebaggery. You'll notice that never once did I
ask anybody to approve of my library choice. I actually do know my way
around JavaScript fairly well, and the main reason that I use jQuery is
for its selectors. My specialty is front-end code, specifically
cross-browser compatible, semantic CSS, and I'm a perfectionist when it
comes to my markup.

You're joking, right? Your claim of "knowing JS fairly well" combined
with your choice of library (plus this very problem, which is a cinch in
plain library-less JS) just don't make any sense.
I don't understand why people have to drag their opinions in when
someone's just looking for some technical help...and your unfamiliarity
with a library's API is no reason to ditch the library. $.fn.attr is
very easy to understand.

So how come when everything is so simple and easy, that you've a problem
(which again isn't one, since it can be dealt with perhaps a dozen of
lines of JS)?
So, all that being said, I much prefer $('.sponsors') to a complicated
series of for loops that aggregate an element collection.

One could - if supported - use document.getElementsByClassName(). My
fallback solution is precisely 6 lines of code.
Sure, I could
write my own getElementsByClassName (or wait for its full adoption in
browsers), but that's the point of using a library. And even that method
wouldn't support the rich set of selectors that the major JavaScript
libraries do.

What do I need all those selectors for? I recently thought about doing my
"universal" selector function. But then I realized, that I've never
really needed one yet.
Your comment about performance is a red herring: I'm not too worried
about performance in a tiny script that rotates the sources of nine
images into a target image.

And you are not too worried about using a 100k script for rotating 9
image sources?
Since you use a rather bloated library (you most likely don't
understand), you can't influence the performance, anyway. And if you've
more complicated tasks at hand or an iPhone as platform you just gonna
have to live with the poor performance.

Gregor
 
D

Dr J R Stockton

In comp.lang.javascript message <24469ef9-9347-4c0f-ad70-49431a393659@k2
g2000yql.googlegroups.com>, Mon, 30 Mar 2009 12:42:18, David Mark
Lines: 155
That's one of the browser sniffing versions, which has been abandoned
as even John Resig knows it is junk at this point. He took yet
another Mulligan with 1.3 (and two more since.)

The ignorant can roll their eyes all they want. Their heads will
follow.

Read FAQ, 1.3, 2, 1.

Thomas Allen : I advise you to sign yourself in full, to reduce risk of
confusion with TL. He's the one who DM emulates.
 
L

Laurent vilday

Thomas Allen :
I much prefer $('.sponsors') to a complicated
series of for loops that aggregate an element collection.

Event delegation (google it) is a much more interesting alternative to both.
 
D

David Mark

In comp.lang.javascript message <24469ef9-9347-4c0f-ad70-49431a393659@k2
g2000yql.googlegroups.com>, Mon, 30 Mar 2009 12:42:18, David Mark



Read FAQ, 1.3, 2, 1.

Would it have been a good idea?
Thomas Allen : I advise you to sign yourself in full, to reduce risk of
confusion with TL.  He's the one who DM emulates.

Try to pay attention, Doc. The other Thomas and I rarely agree on
anything and he certainly doesn't have my style.
 
L

Laurent vilday

Dr J R Stockton :
Thomas Allen : I advise you to sign yourself in full, to reduce risk of
confusion with TL. He's the one who DM emulates.

I strongly disagree. I usually don't care about bashing and bird names,
but I can't let you say such thing, that's not honest.

David is *not* emulating at all Thomas Lhan. TL is an ass, David is
*not*, at least as far I can tell of course since I don't know him
personnaly.

David can seem a little rude, that's true and I even had a few
unfortunates words with him - words I regret since he was right once
again -. But he is mostly (if not always) right, his points are always
correct, he is a *very* valuable source of informations and thinking.

You (anyone) can dislike him, that's individual judgement and can't be
discussed, however you can not deny his huge javascript value.

My javascript skills (if any) have really improved since I read him. I
wish I was half competent as he is, and **** away anyone who is too
intolerant to understand what he is saying.

David has to be listened by everyone, while TL has to be reduced to silence.

DM != TL

My 2 useless cents :)
 
A

Alan Gutierrez

Here's a simpler alternative:

(function($) {
        $(document).ready(function() {
                var sponsors = $('.sponsors');
                var sponsor_images = sponsors.find('img').remove();
                var sponsor_frame = $('<img>');

                for(var i = 0; i < sponsor_images.length; i++) {
                       setTimeout("sponsor_frame.attr('src', $(sponsor_images).attr
('src'))", 3000);
                };
                sponsors.html(sponsor_frame);
        });

}) (jQuery);

However, I don't understand scope inside ofsetTimeout: I get
"undefined" errors for all of the variables in thesetTimeoutcommand,
and I'm not too keen on making each one global.


Thomas

From https://developer.mozilla.org/en/DOM/window.setTimeout

timeoutID = window.setTimeout(code, delay);

timeoutID is the ID of the timeout, which can be used with
window.clearTimeout.

The problem with your first attempt, is that you used setTimeout as if
it was a "future" pattern that would return the value returned from
your function.

The problem with your second attempt is that you are giving setTimeout
some code to run, but that means that the code will be run in the
context of the window, with the this pointer set to the window.

You want to pass a function, so that the context is captured in the
function.

But, what you really want to do is change those images after an
interval, so use setInterval.

var count = 0;
setInterval(function () {
sponsor_frame.attr('src', $(sponsor_images[count]).attr('src'));
if (++count == sponsor_images.length) count = 0;
}, 3000);

Untested. But on the right track.

Alan Gutierrez
 
D

David Mark

Dr J R Stockton :


I strongly disagree. I usually don't care about bashing and bird names,
but I can't let you say such thing, that's not honest.

[snip]

The good "doctor" sees Lahn everywhere. He's been obsessed with the
subject since around 2001 IIRC. I wasn't here back then, but I
stumbled over an old post once that gave me an extreme sense of deja
vu (something about Usenet posting conventions of course.) Other than
the occasional xenophobic rant and time/date musings, it's all he
talks about and the archive has preserved his personality (or lack
thereof) for posterity.
 
G

Garrett Smith

David said:
Dr J R Stockton :
[...]


The good "doctor" sees Lahn everywhere. He's been obsessed with the
subject since around 2001 IIRC. I wasn't here back then, but I
stumbled over an old post once that gave me an extreme sense of deja
vu (something about Usenet posting conventions of course.) Other than
the occasional xenophobic rant and time/date musings, it's all he
talks about and the archive has preserved his personality (or lack
thereof) for posterity.

That's not true. There are a lot of posts about the FAQ that helped
contribute to changing the FAQ. Many of the items in the original FAQ
seemed to originate from him. An earlier edition of the toFixed example,
and others, from six or seven years back, appeared in a post.

He asks questions about dom-related things from time to time.

Garrett
 
D

David Mark

David said:
Dr J R Stockton :
[...]



The good "doctor" sees Lahn everywhere.  He's been obsessed with the
subject since around 2001 IIRC.  I wasn't here back then, but I
stumbled over an old post once that gave me an extreme sense of deja
vu (something about Usenet posting conventions of course.)  Other than
the occasional xenophobic rant and time/date musings, it's all he
talks about and the archive has preserved his personality (or lack
thereof) for posterity.

That's not true. There are a lot of posts about the FAQ that helped
contribute to changing the FAQ. Many of the items in the original FAQ
seemed to originate from him. An earlier edition of the toFixed example,
and others, from six or seven years back, appeared in a post.

Ah yes, numbers too.
He asks questions about dom-related things from time to time.

He should go back to doing that.
 
M

Mike Duffy

.... TL is an ass, David is *not*, at least as far
I can tell of course since I don't know him personnaly.

Do you know TL personally?

I hazard coming to his defence. But although he seems too abrupt for most
people (some more than others as has been pointed out), I find that his
advice is usually correct in terms of fact.
David can seem a little rude, that's true and
even had a few unfortunates words with him - words
I regret since he was right once again -. But he is
mostly (if not always) right, his points are always correct,
he is a *very* valuable source of informations and thinking.

This is what I see from TL, except change "little rude" to "more than a
little too rude for some people to handle". Maybe I'm just to thick-skinned
to pick up all of it. (Some may say too "thick", but I digress...)
 
E

Erwin Moller

Dr J R Stockton schreef:
In comp.lang.javascript message <24469ef9-9347-4c0f-ad70-49431a393659@k2
g2000yql.googlegroups.com>, Mon, 30 Mar 2009 12:42:18, David Mark



Read FAQ, 1.3, 2, 1.

Thomas Allen : I advise you to sign yourself in full, to reduce risk of
confusion with TL. He's the one who DM emulates.

I don't agree with that.
Thomas Lahn and David Mark have very different styles and are not alike.
If Thomas has a bad day he can be rude (especially to newbies), or even
insulting to some.
On good days, however, Thomas takes the time to explain difficult stuff
in-depth in long postings that surely cost him time.
It is my impression that David has (a little) more patience with
(arrogant) newbies.

Above all: Thomas Lahn and David Mark are both valuable sources of
information and reflection when it comes to JavaScript, and that is why
I participate in this group: to learn and to help others.
Posting-style is of much less importance (to me).
Maybe my skin is thick too. ;-)

Just my 2 cent.

Regards,
Erwin Moller

--
"There are two ways of constructing a software design: One way is to
make it so simple that there are obviously no deficiencies, and the
other way is to make it so complicated that there are no obvious
deficiencies. The first method is far more difficult."
-- C.A.R. Hoare

--
"There are two ways of constructing a software design: One way is to
make it so simple that there are obviously no deficiencies, and the
other way is to make it so complicated that there are no obvious
deficiencies. The first method is far more difficult."
-- C.A.R. Hoare
 
D

Dr J R Stockton

Tue said:
That's not true. There are a lot of posts about the FAQ that helped
contribute to changing the FAQ.
Yes.

Many of the items in the original FAQ seemed to originate from him.

Completely untrue. Has exaggerated truth if by "original" you mean
merely before you became FAQ maintainer, or before Randy Webb did.
An earlier edition of the toFixed example, and others, from six or
seven years back, appeared in a post.

Jim Ley wrote that, IIRC - but possibly as a consequence of what I had
written.

FAQ 5.1 code is still not executable.
 

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

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,738
Latest member
JinaMacvit

Latest Threads

Top