469,133 Members | 982 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,133 developers. It's quick & easy.

Why the delay please?

Amy
Hi, I finished this script and for some reason there is a delay
every so often in the timing. Sometimes
it seems two take 2 seconds instead of 1.
Can anyone see anything that would slow it down? I sure can't see it.
Thank you very much, Amy <body onload="changelink()"><a href="#"
id="url"></a>

arr = new Array();
arr[0] = ["Link 1","http://www.google.com"];
arr[1] = ["Link 2","http://www.msn.com"];
arr[2] = ["Link 3","http://www.yahoo.com"];
arr[3] = ["Link 4","http://www.google.com"];
arr[4] = ["Link 5","http://www.msn.com"];
arr[5] = ["Link 6","http://www.yahoo.com"];
arr[6] = ["Link 7","http://www.msn.com"];
arr[7] = ["Link 8","http://www.google.com"];
arr[8] = ["Link 9","http://www.yahoo.com"];
arr[9] = ["Link 10","http://www.msn.com"];
arr[10] = ["Link 11","http://www.yahoo.com"];
arr[11] = ["Link 12","http://www.google.com"];
arr[12] = ["Link 13","http://www.msn.com"];
arr[13] = ["Link 14","http://www.yahoo.com"];
arr[14] = ["Link 15","http://www.google.com"];
counter = 0;

function changelink()
{
rand = Math.floor(Math.random()*arr.length);
document.getElementById("url").href = arr[rand][1];
document.getElementById("url").innerHTML = arr[rand][0];
counter++;

if (counter == arr.length)
counter = 0;
}

setInterval('changelink()', 1000);

Nov 7 '06 #1
17 1609
Lee
Amy said:
>
Hi, I finished this script and for some reason there is a delay
every so often in the timing. Sometimes
it seems two take 2 seconds instead of 1.
Can anyone see anything that would slow it down? I sure can't see it.
Thank you very much, Amy <body onload="changelink()"><a href="#"
id="url"></a>

arr = new Array();
arr[0] = ["Link 1","http://www.google.com"];
arr[1] = ["Link 2","http://www.msn.com"];
arr[2] = ["Link 3","http://www.yahoo.com"];
arr[3] = ["Link 4","http://www.google.com"];
arr[4] = ["Link 5","http://www.msn.com"];
arr[5] = ["Link 6","http://www.yahoo.com"];
arr[6] = ["Link 7","http://www.msn.com"];
arr[7] = ["Link 8","http://www.google.com"];
arr[8] = ["Link 9","http://www.yahoo.com"];
arr[9] = ["Link 10","http://www.msn.com"];
arr[10] = ["Link 11","http://www.yahoo.com"];
arr[11] = ["Link 12","http://www.google.com"];
arr[12] = ["Link 13","http://www.msn.com"];
arr[13] = ["Link 14","http://www.yahoo.com"];
arr[14] = ["Link 15","http://www.google.com"];
counter = 0;

function changelink()
{
rand = Math.floor(Math.random()*arr.length);
document.getElementById("url").href = arr[rand][1];
document.getElementById("url").innerHTML = arr[rand][0];
counter++;

if (counter == arr.length)
counter = 0;
}

setInterval('changelink()', 1000);
Sometimes your computer is busy doing something else.
Can you explain the purpose of the "counter" variable?
This still looks like a homework assignment, to me.
--

Nov 7 '06 #2

Amy wrote:
Hi, I finished this script and for some reason there is a delay
every so often in the timing. Sometimes
it seems two take 2 seconds instead of 1.
Can anyone see anything that would slow it down? I sure can't see it.
arr = new Array();
arr[0] = ["Link 1","http://www.google.com"];
arr[1] = ["Link 2","http://www.msn.com"];
arr[2] = ["Link 3","http://www.yahoo.com"];
arr[3] = ["Link 4","http://www.google.com"];
arr[4] = ["Link 5","http://www.msn.com"];
arr[5] = ["Link 6","http://www.yahoo.com"];
arr[6] = ["Link 7","http://www.msn.com"];
arr[7] = ["Link 8","http://www.google.com"];
arr[8] = ["Link 9","http://www.yahoo.com"];
arr[9] = ["Link 10","http://www.msn.com"];
arr[10] = ["Link 11","http://www.yahoo.com"];
arr[11] = ["Link 12","http://www.google.com"];
arr[12] = ["Link 13","http://www.msn.com"];
arr[13] = ["Link 14","http://www.yahoo.com"];
arr[14] = ["Link 15","http://www.google.com"];
Think about it - what happens if we are displaying the content of
arr[0] and we pick the random number 3 as the next one to display?
Would the script appear to pause?

Nov 7 '06 #3

n...@chthonic.f9.co.uk wrote:
>
Think about it - what happens if we are displaying the content of
arr[0] and we pick the random number 3 as the next one to display?
Would the script appear to pause?
Scratch that - I meant if we pick the random number 0 as the next one
to display.

You can work out for yourself what to do next...

Nov 7 '06 #4
Lee
Amy said:
>
Hi, I finished this script and for some reason there is a delay
every so often in the timing. Sometimes
it seems two take 2 seconds instead of 1.
Can anyone see anything that would slow it down? I sure can't see it.
Thank you very much, Amy <body onload="changelink()"><a href="#"
id="url"></a>

arr = new Array();
arr[0] = ["Link 1","http://www.google.com"];
arr[1] = ["Link 2","http://www.msn.com"];
arr[2] = ["Link 3","http://www.yahoo.com"];
arr[3] = ["Link 4","http://www.google.com"];
arr[4] = ["Link 5","http://www.msn.com"];
arr[5] = ["Link 6","http://www.yahoo.com"];
arr[6] = ["Link 7","http://www.msn.com"];
arr[7] = ["Link 8","http://www.google.com"];
arr[8] = ["Link 9","http://www.yahoo.com"];
arr[9] = ["Link 10","http://www.msn.com"];
arr[10] = ["Link 11","http://www.yahoo.com"];
arr[11] = ["Link 12","http://www.google.com"];
arr[12] = ["Link 13","http://www.msn.com"];
arr[13] = ["Link 14","http://www.yahoo.com"];
arr[14] = ["Link 15","http://www.google.com"];
counter = 0;

function changelink()
{
rand = Math.floor(Math.random()*arr.length);
alert(rand);
>document.getElementById("url").href = arr[rand][1];
document.getElementById("url").innerHTML = arr[rand][0];
counter++;

if (counter == arr.length)
counter = 0;
}

setInterval('changelink()', 1000);
Even if this is an assignment, I suppose it doesn't hurt to
show you how to debug it. Add the alert() above and watch
how the values change. You'll see it more quickly if you
reduce the number of elements in arr while testing.
--

Nov 7 '06 #5
Amy
Thank you very much. This isn't homework at all, I provide all the
javascript almost completely done which is more than most people do.
You guys already seem to know what to do. I still don't know what your
trying to tell me. Can't seem to find that alert you told me to try.
Are you trying to tell me that with random arrays since its random it
takes longer sometimes to pick its next choice? Thats what causes a
delay? If thats the case why don't I have problem with other second
random arrays that I have tested? I'll slow down the timer and see what
happens and let you know. If there is something else I am messing
please tell me what it is.

Lee wrote:
Amy said:

Hi, I finished this script and for some reason there is a delay
every so often in the timing. Sometimes
it seems two take 2 seconds instead of 1.
Can anyone see anything that would slow it down? I sure can't see it.
Thank you very much, Amy <body onload="changelink()"><a href="#"
id="url"></a>

arr = new Array();
arr[0] = ["Link 1","http://www.google.com"];
arr[1] = ["Link 2","http://www.msn.com"];
arr[2] = ["Link 3","http://www.yahoo.com"];
arr[3] = ["Link 4","http://www.google.com"];
arr[4] = ["Link 5","http://www.msn.com"];
arr[5] = ["Link 6","http://www.yahoo.com"];
arr[6] = ["Link 7","http://www.msn.com"];
arr[7] = ["Link 8","http://www.google.com"];
arr[8] = ["Link 9","http://www.yahoo.com"];
arr[9] = ["Link 10","http://www.msn.com"];
arr[10] = ["Link 11","http://www.yahoo.com"];
arr[11] = ["Link 12","http://www.google.com"];
arr[12] = ["Link 13","http://www.msn.com"];
arr[13] = ["Link 14","http://www.yahoo.com"];
arr[14] = ["Link 15","http://www.google.com"];
counter = 0;

function changelink()
{
rand = Math.floor(Math.random()*arr.length);
alert(rand);
document.getElementById("url").href = arr[rand][1];
document.getElementById("url").innerHTML = arr[rand][0];
counter++;

if (counter == arr.length)
counter = 0;
}

setInterval('changelink()', 1000);

Even if this is an assignment, I suppose it doesn't hurt to
show you how to debug it. Add the alert() above and watch
how the values change. You'll see it more quickly if you
reduce the number of elements in arr while testing.
--
Nov 7 '06 #6
Amy
Are you trying to tell me I don't need the counter variable since its
random?

Making it a longer timer didn't help, it doubles the timing on some
items no matter what.

Nov 7 '06 #7
Amy
Don't know if thats what you are trying to tell me or not but makes
perfect sense so I took all the counters out. To this.

function changelink()
{
var rand = Math.floor(Math.random()*content.length);
document.getElementById("url").href = content[rand][1];
document.getElementById("url").innerHTML = content[rand][0];
}

setInterval("changelink()", 2000);

Same problem though, still a delay. I sure hope someone can help.

Nov 7 '06 #8
Lee
Amy said:
>
Thank you very much. This isn't homework at all, I provide all the
javascript almost completely done which is more than most people do.
You guys already seem to know what to do. I still don't know what your
trying to tell me. Can't seem to find that alert you told me to try.
Are you trying to tell me that with random arrays since its random it
takes longer sometimes to pick its next choice? Thats what causes a
delay? If thats the case why don't I have problem with other second
random arrays that I have tested? I'll slow down the timer and see what
happens and let you know. If there is something else I am messing
please tell me what it is.
If the same random number is generated twice in a row, the
link won't change, and so it will appear to be taking longer.
The following includes simple code to ensure that there are
no repeats by incrementing the value if necessary. You could
just create random numbers until you find one that's not the
same as the previous one, but this way has the advantage that
it even works if arr.length=1. We sacrifice some randomness
for that, but it's a good bargain in this case. Another way
to go would be to generate a shuffled list of indices and
iterating through it, so you never see a repeat until you've
seen all of the links, but that's more complicated.
You'll find more than you probably want to know at:
http://www.merlyn.demon.co.uk/js-randm.htm
var previous=-1;
function changelink() {
var rand = Math.floor(Math.random()*arr.length);
if ( rand == previous ) {
rand++;
if ( rand == arr.length ) {
rand=0;
}
}
previous=rand;
document.getElementById("url").href = arr[rand][1];
document.getElementById("url").innerHTML = arr[rand][0];
}
--

Nov 7 '06 #9
Amy wrote on 07 nov 2006 in comp.lang.javascript:
Don't know if thats what you are trying to tell me or not but makes
perfect sense so I took all the counters out. To this.
What are you talking about, Amy?

Could you please adhere to usenet netiquette and quote what you are
reacting on, as this is not email but usenet posting.

[and when you quote, please do not toppost]

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Nov 7 '06 #10
Amy
Thank you very, very much Lee for the help, I'm trying it right now.

Evertjan, you tell me that I should do this and that, like I am doing
something hugely wrong. In my opinion you need to relax instead. All I
was doing was replying to all of your suggestions, that were one right
after the other and easy to track. There aren't so many posts in this
thread that there is a lot of confusion. Many of us in this life try
not to sweat this kind of small stuff that some get so picky about in
forums, and worry about much bigger concerns instead. Please don't try
pull me into conflicts over such tiny issues. Also, you can tell people
in much nicer ways without the edge. Regarding top posting, I think you
mean not replying to the very last post, I didn't know I was doing
that. But I'll keep an eye on it in case I am.
Could you please adhere to usenet netiquette and quote what you are
reacting on, as this is not email but usenet posting.>
[and when you quote, please do not toppost]
Nov 7 '06 #11
Amy wrote on 07 nov 2006 in comp.lang.javascript:
Thank you very, very much Lee for the help, I'm trying it right now.

Evertjan, you tell me that I should do this and that, like I am doing
something hugely wrong. In my opinion you need to relax instead. All I
was doing was replying to all of your suggestions, that were one right
after the other and easy to track. There aren't so many posts in this
thread that there is a lot of confusion. Many of us in this life try
not to sweat this kind of small stuff that some get so picky about in
forums, and worry about much bigger concerns instead. Please don't try
pull me into conflicts over such tiny issues. Also, you can tell people
in much nicer ways without the edge. Regarding top posting, I think you
mean not replying to the very last post, I didn't know I was doing
that. But I'll keep an eye on it in case I am.
>Could you please adhere to usenet netiquette and quote what you are
reacting on, as this is not email but usenet posting.>
[and when you quote, please do not toppost]
[Please do not toppost on usenet]

What a long reply for saying "thank you for allerting me to the habits of
usenet".

No, the quirks of usenet make it impossible to know that all prior postigs
have arrived or are still available on the specific newsserver of a
specific user. Thet's why those three mails without quoting are something
that should be pointed out to you.

No, having some order in a NG is not a trifle complared to some delay.

Should I be politer than saying please twice?

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Nov 7 '06 #12
Amy
I guess I don't know what usenet is because I have no idea what your
talking about.
>From an average computer all these posts show up in the order they are
received, so if there is something I am missing you'll have to tell me
what it is. This is where we seem to still differ the most, your even
small critique on me was packed with edge. You could have told me a lot
nicer. Telling me I am using this as email etc. was all very mean.
People can use please in their posts but if it is also packed a lot of
sarcasm that totally cancels out "please."

Nov 7 '06 #13
Amy
I hope all that didn't chase you off Lee, I could still really use some
of your advise so I can try other things.

Its not working yet, I thought maybe because of that I should put the
"if" statements on the end, that didn't help. Is one "if" statement
supposed to be inside of the other?

Here's how it looks now. I am getting an error on the page with no
functions yet. Can anyone see what is wrong with this? Thanks a bunch.

var previous=-1;
function changelink() {
var rand = Math.floor(Math.random()*arr.length);
previous=rand;
document.getElementById("url").href = arr[rand][1];
document.getElementById("url").innerHTML = arr[rand][0];
if ( rand == previous ) {
rand++;
if ( rand == arr.length ) {
rand=0;
}
}
}

Nov 7 '06 #14
Lee
Amy said:
>
I hope all that didn't chase you off Lee, I could still really use some
of your advise so I can try other things.

Its not working yet, I thought maybe because of that I should put the
"if" statements on the end, that didn't help. Is one "if" statement
supposed to be inside of the other?
The function should look just like this:

function changelink() {
var rand = Math.floor(Math.random()*arr.length);
if ( rand == previous ) {
rand++;
if ( rand == arr.length ) {
rand=0;
}
}
previous=rand;
document.getElementById("url").href = arr[rand][1];
document.getElementById("url").innerHTML = arr[rand][0];
}

If you're getting errors from that, let us know what error
messages you're getting and post the exact code that caused
the errors.

By the way, "all these posts" are part of USENET. It's been
around much longer than Microsoft Windows or the World Wide Web,
and people use all sorts of different software to read the
messages, so we don't really know what you mean by how it looks
on an "average computer".

Notice how I include part of what you said in my response, and
then put my response at the bottom? That's what you're being
asked to do. People should be able to read any single message
and at least be able to decide if they're able to help. Ideally
they should have all the information they need.

You should also make sure you're responding to the correct
message. I almost didn't read this message from you because
it wasn't a response to my last message.

This newsgroup isn't a friendly place.
You'll have to get used to that.
--

Nov 7 '06 #15
Amy
At least Lee you're trying to be nice when telling me what I don't
understand. Thanks for the warning. I'm not here to try to make people
be nice, if they haven't figured that out all on their own thats their
loss in their life. I will tell someone if they are the ones to start
with an attitude first. I guess your telling me that usenet can't see
all these posts in the order they are given. I didn't know that and
will find out more about, thanks for telling me. I gave you all my code
including whats at the top of this post, its still the same. The error
message I am getting says "previous" is undefined.

Nov 7 '06 #16
Amy
Don't sweat it Lee some other nice person sent me something. Thanks for
trying to help.

Nov 7 '06 #17
In message <11**********************@m7g2000cwm.googlegroups. com>, Mon,
6 Nov 2006 18:44:00, Amy <ri*******@aol.comwrites
>Hi, I finished this script and for some reason there is a delay
every so often in the timing. Sometimes
it seems two take 2 seconds instead of 1.
Can anyone see anything that would slow it down? I sure can't see it.
Thank you very much, Amy <body onload="changelink()"><a href="#"
id="url"></a>

arr = new Array();
arr[0] = ["Link 1","http://www.google.com"];
arr[1] = ["Link 2","http://www.msn.com"];
...
arr[13] = ["Link 14","http://www.yahoo.com"];
arr[14] = ["Link 15","http://www.google.com"];
counter = 0;

function changelink()
{
rand = Math.floor(Math.random()*arr.length);
document.getElementById("url").href = arr[rand][1];
document.getElementById("url").innerHTML = arr[rand][0];
counter++;

if (counter == arr.length)
counter = 0;
}

setInterval('changelink()', 1000);
(A) With 15 entries in the array, on the average once in 15 times the
current entry will be picked next time.

(B) In some systems, setInterval('...', 1000); will fire at intervals
averaging slightly over one second - see FAQ section 3.2.
If your test system allows writing to window.status, include
window.status++ in changelink; it will show when your timer fires.

Check your articles before posting, and do not write "two" where "too"
is appropriate. If you are not a native English user, provide some
indication : those who do not are by default assumed to be American or
to deserve to be considered as such - and the dominant language there is
not unlike English.
In message <11********************@m7g2000cwm.googlegroups.co m>, Tue, 7
Nov 2006 11:14:03, Amy <ri*******@aol.comwrites
>
Evertjan, you tell me that I should do this and that, like I am doing
something hugely wrong.
That's because you are.

Being pig-headed is expected, but not mandatory, for Google Groups
users. Others have learned to avoid such faults. See St Luke: Chapter
10, Verse 37, tail.

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

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Nov 8 '06 #18

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by Des L. Davis | last post: by
6 posts views Thread by adish11 | last post: by
11 posts views Thread by Maheshkumar.R | last post: by
4 posts views Thread by FAQ server | last post: by
1 post views Thread by maul581 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Mortomer39 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.