Forum Moderators: open

Message Too Old, No Replies

Anyone know how to javascript loop?

         

Simone100

5:14 am on Oct 23, 2006 (gmt 0)

10+ Year Member



Hell, I have one problem left with this script. I have the wrong looping command. Can someone give me a better one? It gets to the end of the array and then it doesn't start over. The "if" command is listed below in function change(). It is the only if command in the whole script so am sure this is where the problem is. How can I change the if command to make the array start over, once it gets to the end? Thanks a heap.

arr = new Array(

["http://www.google.com/test.jpg"],
["http://www.google.com/test2.jpg"]
);

now = new Date()
nowDate = now.getMinutes()

function change(){
if(nowDate > arr.length) { return; }}

daveVk

7:29 am on Oct 23, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



if(nowDate > arr.length) { nowDate=0; }

maybe?

Simone100

8:25 am on Oct 23, 2006 (gmt 0)

10+ Year Member



Didn't work, but thanks!

RonPK

9:20 am on Oct 23, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What is the script supposed to do?

Simone100

9:49 am on Oct 23, 2006 (gmt 0)

10+ Year Member



Its just supposed to show an image every second. Here I added this to it and it works fine except its not starting over when it gets to the end.

now = new Date()
nowDate = now.getSeconds()
function change(){
document.getElementById("img").src = arr[new Date().getSeconds() - 1][0]
setTimeout('changeImg()',999)
}

Would if I add a variable to it like...
var conter=0.

And then an if comment or something that if counter=0. The script keeps going. I just don't know how to say "keep the script going."
if(counter=0 {?} Hope someone can help.

Simone100

10:31 am on Oct 23, 2006 (gmt 0)

10+ Year Member



Or How would I say if array is longer than 59 to start over?
if(arr.length>59) { arr; }

daveVk

11:15 am on Oct 23, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



where arr declared add

var conter=0;

to kick things off

changeImg();

replace changeImg with

function changeImg() {
document.getElementById("img").src = arr[conter];
conter++;
if ( conter == arr.length ) { conter = 0; }
setTimeout('changeImg()',1000);
}

Simone100

1:37 pm on Oct 23, 2006 (gmt 0)

10+ Year Member



oh man Dave, sure hope your onto something, I'll try it thanks.

Simone100

1:41 pm on Oct 23, 2006 (gmt 0)

10+ Year Member



Dave, I don't think that will work. I want the image to show on the minute it is on so they always are different. Do you have a way I can still start the loop over with nearly what I have? Please let me know, thank you very much!

daveVk

12:34 am on Oct 24, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Can you state your requirement more clearing, do you wish to sequence thru set of images at some fixed time interval or is it important that particular image shows on particular minute of the hour like a clock display.

Simone100

3:00 am on Oct 24, 2006 (gmt 0)

10+ Year Member



Thanks Dave, yes the one I give at the beginning is set to appear on the minute so when the minute changes the image will show on the next minute and so on. The setTimout is what makes them change on their own, otherwise they only change by minute on page load. But when they get to image 59 they stop, they don't keep going.

arr = new Array(

["http://www.google.com/test.jpg"],
["http://www.google.com/test2.jpg"]
);

now = new Date()
nowDate = now.getMinutes()

function changeImg(){
document.getElementById("img").src = arr[new Date().getSeconds() - 1setTimeout('changeImg()',999)
}

I tried adding a while or if command do the function.

while (arr.lenght<59){function changeImg(){
document.getElementById("img").src = arr[new Date().getSeconds() - 1setTimeout('changeImg()',999)
}}

But they don't work. Hope you can help.

Simone100

3:02 am on Oct 24, 2006 (gmt 0)

10+ Year Member



Sorry, about the seconds in the previous post instead of minutes, I was testing it by seconds too so I could test it faster. It doesn't change anything, same problem, with seconds or minutes.

Simone100

3:15 am on Oct 24, 2006 (gmt 0)

10+ Year Member



If you need to test it. This goes in the body.

<BODY onload="changeImg();"><img id="img" border="0"/>

This goes in external.

arr = new Array(

["http://www.google.com/test.jpg"],
["http://www.google.com/test2.jpg"]
);

now = new Date()
nowDate = now.getSeconds()

function changeImg(){
document.getElementById("img").src = arr[new Date().getSeconds() - 1][0]
setTimeout('changeImg()',999)
}

daveVk

4:48 am on Oct 24, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The code as per your last post looks fine, but you will need 60 urls in arr, one for each minute or second whatever. Also remove -1 here, getSeconds returns number in range 0 to 59.

document.getElementById("img").src = arr[new Date().getSeconds()][0]

these lines can go as nowDate no longer used?

now = new Date()
nowDate = now.getSeconds()

Simone100

7:41 am on Oct 24, 2006 (gmt 0)

10+ Year Member



Hey Dave you did it! Regarding my javascript loop. Thank you so much! I had no idea the problem was in that area, I thought I needed a loop command. I have two other problems I am having, that I am hoping you can be the one to help me with because you prooved you know what you are doing. One is similar to this only regarding a count method which it looks like you could fix in probably five minutes do to some information you gave me earlier in the thread. I thought it was kind of funny you were giving me information on another problem I was having without me even asking.

The second problem totally unrelated is with a script I am trying to adjust to help me with day of the year. If you could help me with these two items I'll need to have someone send you a check for 20.00 for your services. I wouldn't want you to help me with those things for free. The day of the year item, I believe I have most of what you will need for that as well, just having trouble putting a few pieces together correctly. Please let me know if you will help me with this. Thank you Dave. Simone.

Simone100

5:53 am on Oct 26, 2006 (gmt 0)

10+ Year Member



Here's his reply and then all the code again. I am going to ask him if he knows how to get each day of the year, not just a specific date. I'll let you know whether or not he gets back to me.

The first piece of code I gave you was a function I wrote. It contains code which tells your JavaScript interpreter how to calculate the day of the year for a date.

The second piece of code relies on the first piece of code, and is an example showing how the first piece of code works.
First:

function dayofyear(d) { // d is a Date object
var yn = d.getFullYear();
var mn = d.getMonth();
var dn = d.getDate();
var d1 = new Date(yn,0,1,12,0,0); // noon on Jan. 1
var d2 = new Date(yn,mn,dn,12,0,0); // noon on input date
var ddiff = Math.round((d2-d1)/864e5);
return ddiff+1; }

Second:
The usage of my function is as follows:

Code:

var a=new Date("July 1, 2007");
var b=dayofyear(a);
alert(b);

If my math is correct, and if the function is correct, this will give you the number 182.

Simone100

6:47 am on Oct 26, 2006 (gmt 0)

10+ Year Member



OK this may be the answer! ...or maybe not but he sounds VERY confident about it.

This is the reply for each date.

Simple. Just grab today's date and stick it in.
Code:

var curdate=new Date(); // gets today's date
var cdnum=dayofyear(curdate);
document.write('Today is day number '+cdnum+' of the current year.');

By the way, my code is designed to always count Jan. 1 as day number 1.

Hope that helps, Simone