Why the delay please?

Discussion in 'Javascript' started by Amy, Nov 7, 2006.

  1. Amy

    Amy Guest

    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);
    Amy, Nov 7, 2006
    #1
    1. Advertising

  2. Amy

    Lee Guest

    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.


    --
    Lee, Nov 7, 2006
    #2
    1. Advertising

  3. Amy

    9.co.uk Guest

    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?
    9.co.uk, Nov 7, 2006
    #3
  4. Amy

    9.co.uk Guest

    9.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...
    9.co.uk, Nov 7, 2006
    #4
  5. Amy

    Lee Guest

    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.


    --
    Lee, Nov 7, 2006
    #5
  6. Amy

    Amy Guest

    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.
    >
    >
    > --
    Amy, Nov 7, 2006
    #6
  7. Amy

    Amy Guest

    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.
    Amy, Nov 7, 2006
    #7
  8. Amy

    Lee Guest

    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];
    }


    --
    Lee, Nov 7, 2006
    #8
  9. Amy

    Amy Guest

    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.
    Amy, Nov 7, 2006
    #9
  10. Amy

    Evertjan. Guest

    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)
    Evertjan., Nov 7, 2006
    #10
  11. Amy

    Amy Guest

    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]
    Amy, Nov 7, 2006
    #11
  12. Amy

    Evertjan. Guest

    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)
    Evertjan., Nov 7, 2006
    #12
  13. Amy

    Amy Guest

    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."
    Amy, Nov 7, 2006
    #13
  14. Amy

    Amy Guest

    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;
    }
    }
    }
    Amy, Nov 7, 2006
    #14
  15. Amy

    Lee Guest

    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.


    --
    Lee, Nov 7, 2006
    #15
  16. Amy

    Amy Guest

    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.
    Amy, Nov 7, 2006
    #16
  17. Amy

    Amy Guest

    Don't sweat it Lee some other nice person sent me something. Thanks for
    trying to help.
    Amy, Nov 7, 2006
    #17
  18. In message <>, Mon,
    6 Nov 2006 18:44:00, Amy <> writes
    >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 <>, Tue, 7
    Nov 2006 11:14:03, Amy <> writes
    >
    >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.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
    Dr J R Stockton, Nov 8, 2006
    #18
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. KK
    Replies:
    2
    Views:
    550
    Big Brian
    Oct 14, 2003
  2. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    878
    Mark Rae
    Dec 21, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,840
    Smokey Grindel
    Dec 2, 2006
  4. dwhall
    Replies:
    2
    Views:
    230
    dwhall
    Dec 12, 2007
  5. Daku
    Replies:
    3
    Views:
    1,022
    Gerhard Hoffmann
    Dec 17, 2009
Loading...

Share This Page