Why won't this array loop?

A

Amy

Hello, I have an array with 60 items in it, one for every second, But
when it gets to the end of the 60 items it stops. How do I get it to
start over when it gets to the end of the array? Please let me know,
thank you very much.

arr = new Array(
["Text"],["http://www.website.com"],//to 60 items.
["Text"],["http://www.website.com"]
);


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

VK

Amy said:
Hello, I have an array with 60 items in it, one for every second, But
when it gets to the end of the 60 items it stops. How do I get it to
start over when it gets to the end of the array? Please let me know,
thank you very much.

arr = new Array(
["Text"],["http://www.website.com"],//to 60 items.
["Text"],["http://www.website.com"]
);

For this purpose a "two-dimensional" array is more benefitial:

<html>
<head>
<title>Dynamic Link</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<script type="text/javascript">

var arr = [
['Text 1', '#URL1'],
['Text 2', '#URL2'],
['Text 3', '#URL3']
];

var dynLink = null;

function init() {
dynLink = document.getElementById('url');
window.setTimeout('changeURL(0)',1000);
}

function changeURL(i) {
if (i >= arr.length) {
i = 0;
}
dynLink.innerHTML = arr[0];
dynLink.href = arr[1];
window.setTimeout('changeURL('+(++i)+')',1000);
}

window.onload = init;
</script>
</head>

<body>
<p><a id="url" href="">URL</a></p>
</body>
</html>
 
A

Amy

Thank you very, very much for your help. I guess it doesn't have to
happen right on the particular second. But if anyone comes up with a
easy way to make my other one loop please let me know. Thats the only
thing wrong with it.
VK said:
Amy said:
Hello, I have an array with 60 items in it, one for every second, But
when it gets to the end of the 60 items it stops. How do I get it to
start over when it gets to the end of the array? Please let me know,
thank you very much.

arr = new Array(
["Text"],["http://www.website.com"],//to 60 items.
["Text"],["http://www.website.com"]
);

For this purpose a "two-dimensional" array is more benefitial:

<html>
<head>
<title>Dynamic Link</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<script type="text/javascript">

var arr = [
['Text 1', '#URL1'],
['Text 2', '#URL2'],
['Text 3', '#URL3']
];

var dynLink = null;

function init() {
dynLink = document.getElementById('url');
window.setTimeout('changeURL(0)',1000);
}

function changeURL(i) {
if (i >= arr.length) {
i = 0;
}
dynLink.innerHTML = arr[0];
dynLink.href = arr[1];
window.setTimeout('changeURL('+(++i)+')',1000);
}

window.onload = init;
</script>
</head>

<body>
<p><a id="url" href="">URL</a></p>
</body>
</html>
 
L

Lee

Amy said:
Hello, I have an array with 60 items in it, one for every second, But
when it gets to the end of the 60 items it stops. How do I get it to
start over when it gets to the end of the array? Please let me know,
thank you very much.

arr = new Array(
["Text"],["http://www.website.com"],//to 60 items.
["Text"],["http://www.website.com"]
);


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

It looks to me as if it won't get through all 60 before it
stops due to a run-time error when your indices go outside
the bounds of your assigned values.

Take a moment to figure out what you want your indices to be
in order to display the pair of (text,URL) values for second
number 0, then for second number 1, etc. You'll see that the
pairs of values that you need are (0,1), (2,3), (4,5), ...
Now check to see what values your code is generating for those
same seconds. They're not the same. Also check the values
that you need vs the values that you're generating for second
58 and second 59.

You also take a chance by calling getSeconds() twice. It's
very possible for the value to change between calls, resulting
in a mismatch between text and URL values. You won't see that
very often, but on a very popular website it could happen just
often enough to get a few complaints.

And there's no reason to make each element of the array "arr"
a array containing one element.


--
 
R

Randy Webb

Amy said the following on 10/29/2006 3:40 AM:
Hello, I have an array with 60 items in it, one for every second, But
when it gets to the end of the 60 items it stops.

It *should* stop when the seconds of the Date object is 0.
How do I get it to start over when it gets to the end of the array?
Please let me know, thank you very much.

arr = new Array(
["Text"],["http://www.website.com"],//to 60 items.
["Text"],["http://www.website.com"]
);


function change()
{
document.getElementById("url").href = arr[new
Date().getSeconds()-1][0];

When new Date().getSeconds() hits 0 (at the beginning of a new minute),
then it subtracts one from that and gets -1, there is no -1 entry in
your array so the browser throws an error and halts script execution.

The simplest way to "fix it" is to redefine the way your array is
defined. You can look up Array Literal definitions to see how to shorten
the array definition but if you define it something like this:

var arr = new Array()
arr[0] = new Array('Text for 0','URL for 0');
arr[1] = new Array('Text for 1','URL for 1');
....
arr[58] = new Array('Text for 58','URL for 58');
arr[59] = new Array('Text for 59','URL for 59');

And then change your function:

function change()
{
refToLink = document.getElementById("url");
currentSeconds = new Date().getSeconds();
refToLink.href = arr[currentSeconds][0];
refToLink.innerHTML = arr[currentSeconds][1];
}

And then instead of using setTimeout, use setInterval:

window.setInterval('change()',1000);

Make sure the setInterval statement is *outside* the function.
 
D

Dr J R Stockton

Amy said:
function change()
{
document.getElementById("url").href = arr[new
Date().getSeconds()-1][0];
document.getElementById("url").innerHTML = arr[new
Date().getSeconds()-2][0];
setTimeout('change()',1000);
}
You also take a chance by calling getSeconds() twice. It's
very possible for the value to change between calls, resulting
in a mismatch between text and URL values. You won't see that
very often, but on a very popular website it could happen just
often enough to get a few complaints.

To clarify : there's no error resulting from calling getSeconds() more
than once on a given Date Object, though it would be more efficient to
store the result in a variable. The source of error is calling new
Date() more than once in an action (when not measuring the passage of
time). The above should start function change() { var D = new Date()
and use D thereafter.

Note that calling setTimeout( , 1000) will in some systems give an
average delay exceeding 1000 ms of the local clock. If every second is
needed, recalculate the delay each time, as in my js-date2.htm#RC and
js-date0.htm#TaI.

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

Lee

Dr J R Stockton said:
Amy said:
function change()
{
document.getElementById("url").href = arr[new
Date().getSeconds()-1][0];
document.getElementById("url").innerHTML = arr[new
Date().getSeconds()-2][0];
setTimeout('change()',1000);
}
You also take a chance by calling getSeconds() twice. It's
very possible for the value to change between calls, resulting
in a mismatch between text and URL values. You won't see that
very often, but on a very popular website it could happen just
often enough to get a few complaints.

To clarify : there's no error resulting from calling getSeconds() more
than once on a given Date Object, though it would be more efficient to
store the result in a variable.

You're right, of course, I started to go on about the inefficiency
of calling the method twice, and then let my mind wander.


--
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top