Jump to content

Why won't this loop?


11Tami

Recommended Posts

Hi can anyone tell me why the array doesn't start over when it gets to the end? Thank you very much. Tami


<BODY onload="changelink();"><a href="#" id="url"></a>

arr = new Array(
["link name"],["http://www.google.com"],
["another link name"],["http://www.yahoo.com"]);

function changelink()   
{document.getElementById("url").href = arr[new Date().getSeconds()-1][0]; 
document.getElementById("url").innerHTML = arr[new Date().getSeconds()-2][0];   
setTimeout('changelink()',1000);}
Link to comment
Share on other sites

I have only two items in the array just as an example. If you try to run the script with 60 items for every second when it get to the 60th item it stops.
Its supposed to start over when it gets to the end of the array and it doesn't. Can anyone help me fix it?
Thank you very much. Tami

Link to comment
Share on other sites

Sounds to me like what you really want is setInterval rather than setTimeout, since the latter will only run once.  Also, assuming that's the same order you have the array in the actual script
[code]
document.getElementById("url").href = arr[new Date().getSeconds()][1]; 
document.getElementById("url").innerHTML = arr[new Date().getSeconds()][0];
[/code]
should give you the right items.  new Date().getSeconds()-1 and 2 is going to kill everything with an error at 0 and 1 seconds.
Hope that's what you need, I'm new here since I'm finally just starting to attempt learning some PHP, but I think I'm alright at the client-side stuff.
Link to comment
Share on other sites

Man you both make a great team and are both very nice, I appreciate the help. The looping only meant when it got to the end of the array it wouldn't start over but now it does. Between the two of your ideas it is almost working. Tele's adjusting my string, and keeping setTimeout like Fenway said, the two combinations work very well together. Now I am having trouble with my array, it was incorrect before it wasn't combining the two sides. I tried this but somethings still wrong, can you see what it is?

Then I'll send you both something for helping me.
Problem still with this, can't see a problem but there is one.
arr = new Array[
  ["Link name google","http://www.google.com"],
  ["Link name two","http://www.yahoo.com"]
  ];
Link to comment
Share on other sites

Gosh none of those work, seems like they should. You sure there isn't some sort of hidden conflict in this somewhere?    -----> arr

arr = new Array(
["Link name google","http://www.google.com"],
["Link name yahoo","http://www.yahoo.com"],
["Link name msn","http://www.msn.com"]
);

arr = [
["Link name google","http://www.google.com"],
["Link name yahoo","http://www.yahoo.com"],
["Link name msn","http://www.msn.com"]];

Link to comment
Share on other sites

Through the magic of copying and pasting twenty times I have it ticking away right now, but being half asleep as usual I forgot to to remove the comma at the end of the array lol and that little typo took me about twenty minutes to figure out.  I'm betting it's something similar that's the only problem now.
Link to comment
Share on other sites

Man I would think so too, looks fine but somethings wrong. Commas all look correct. Strange thing is when I put in an incorrect array like this in it works.

arr = new Array(
["link name"],["http://www.google.com"],
["another link name"],["http://www.yahoo.com"]);

When I switch the above to a correct array like that below then it doesn't work. I don't get it. If anyone that knows javascript and has the time and can test it maybe you can see whats wrong.

Here's the whole thing as it seems like it should work.

<body onload="changelink()"><a href="#" id="url"></a>

arr = new Array(
["Link name google","http://www.google.com"],
["Link name yahoo","http://www.yahoo.com"],
["Link name msn","http://www.msn.com"]
);


function changelink()   
{
document.getElementById("url").href = arr[new Date().getSeconds()][1]; 
document.getElementById("url").innerHTML = arr[new Date().getSeconds()][0];
setTimeout('changelink()',1000);}

Link to comment
Share on other sites

I'm using this in script tags the head and I get no problems:
[code]arr = new Array(
["Link name google","http://www.google.com"],
["Link name yahoo","http://www.yahoo.com"],
["Link name msn","http://www.msn.com"],
["Link name google","http://www.google.com"],
["Link name yahoo","http://www.yahoo.com"],
["Link name msn","http://www.msn.com"],
["Link name google","http://www.google.com"],
["Link name yahoo","http://www.yahoo.com"],
["Link name msn","http://www.msn.com"],
["Link name google","http://www.google.com"],
["Link name yahoo","http://www.yahoo.com"],
["Link name msn","http://www.msn.com"],
["Link name google","http://www.google.com"],
["Link name yahoo","http://www.yahoo.com"],
["Link name msn","http://www.msn.com"],
["Link name google","http://www.google.com"],
["Link name yahoo","http://www.yahoo.com"],
["Link name msn","http://www.msn.com"],
["Link name google","http://www.google.com"],
["Link name yahoo","http://www.yahoo.com"],
["Link name msn","http://www.msn.com"],
["Link name google","http://www.google.com"],
["Link name yahoo","http://www.yahoo.com"],
["Link name msn","http://www.msn.com"],
["Link name google","http://www.google.com"],
["Link name yahoo","http://www.yahoo.com"],
["Link name msn","http://www.msn.com"],
["Link name google","http://www.google.com"],
["Link name yahoo","http://www.yahoo.com"],
["Link name msn","http://www.msn.com"],
["Link name google","http://www.google.com"],
["Link name yahoo","http://www.yahoo.com"],
["Link name msn","http://www.msn.com"],
["Link name google","http://www.google.com"],
["Link name yahoo","http://www.yahoo.com"],
["Link name msn","http://www.msn.com"],
["Link name google","http://www.google.com"],
["Link name yahoo","http://www.yahoo.com"],
["Link name msn","http://www.msn.com"],
["Link name google","http://www.google.com"],
["Link name yahoo","http://www.yahoo.com"],
["Link name msn","http://www.msn.com"],
["Link name google","http://www.google.com"],
["Link name yahoo","http://www.yahoo.com"],
["Link name msn","http://www.msn.com"],
["Link name google","http://www.google.com"],
["Link name yahoo","http://www.yahoo.com"],
["Link name msn","http://www.msn.com"],
["Link name google","http://www.google.com"],
["Link name yahoo","http://www.yahoo.com"],
["Link name msn","http://www.msn.com"],
["Link name google","http://www.google.com"],
["Link name yahoo","http://www.yahoo.com"],
["Link name msn","http://www.msn.com"],
["Link name google","http://www.google.com"],
["Link name yahoo","http://www.yahoo.com"],
["Link name msn","http://www.msn.com"],
["Link name google","http://www.google.com"],
["Link name yahoo","http://www.yahoo.com"],
["Link name msn","http://www.msn.com"]
);
function changelink(){
document.getElementById("url").href = arr[new Date().getSeconds()][1]; 
document.getElementById("url").innerHTML = arr[new Date().getSeconds()][0]; 
setTimeout('changelink()',1000);
}[/code]
And this inside the body:
[code]<a href="#" id="url"></a> [/code]
It works just fine for me in several browsers so personally I have no idea what else to say.  ???
Link to comment
Share on other sites

LOL Fenway, have me giggling over here, your funny. Well thats a relief that made no sense. Thanks a lot Tel. Every many many months, things like this happen for no apparant reason. Last night I restarted the computer for another reason and its working fine now. I don't think my script was resaving properly. You guys did it! Thanks for help. I'll send you something at least.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.