The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

My setTimeout isn't executing.

Hello, where does the setTimeout in this go please, where I have it isn't changing to the next text link automatically and I don't know of a better place to put it. This sees what day of the year it is and shows the text link for today. But its supposed to change to the next item automatically on the next day before you on the hour, but its only changing on page refresh. Please let me know what I am doing wrong, thanks very much.

I've got a clock that uses a setTimeout at 1000 so it can keep track of seconds. I'll check the code, but I think you're doing it basically right. If I read this correct, the script will run every hour and post whichever of the links your function "a" now specifies? Have you really tested it by letting the page sit there for an hour?? That's dedication, let me tell you!

Whatever you can do or dream you can, begin it.
Boldness has genius, power and magic in it. Begin it now. Chroniclemaster1, Founder of Earth ChronicleA Growing History of our Planet, by our Planet, for our Planet.

LOL thank you so much Chroniclemaster1!!! I'm so tired of looking at this code! Hope you can spot it. Actually I wait to see what happens to it at midnight, so can you test it without staying up till midnight?

It changes to a new link every day and also just fine on load, but I'm trying to get it to change to a new link every day automatically, with the setTimeout.

I only made the setTimeout for every hour instead of every day, only because I thought it might change better at midnight that way, but I suppose it doesn't matter if the setTimeout if for every hour or a whole day.

Waiting to see what your test turns up, let me know if you need the whole array, you shouldn't have to write 365 lines if I already have them written! Appreciated very much, Jen

I've been told the reason this isn't working is because of all the math. Since the code isn't a straight forward get an item by day type thing the setTimeout is confused. So I'm going to try something else, thanks.

2. Please correct me if I'm wrong (it always makes me nervous to respond after someone with 1,000+ replies jumps into the conversation ) But I thought the whole purpose of timeout was to make your JS stay active on someone's computer. You don't need to do that at ALL in your case.

If someone opens up your webpage, the function will run properly and then setTimeout will kick in. An hour later when setTimeout finishes counting, it will run the function again. If someone doesn't leave your webpage up for an hour, it never reruns links(). That's what your setTimeout does, it reruns a function everytime it finishes counting.

Maybe that's what you want to do. But if all you want to do is have the link update each day, setTimeout is completely unnecessary. You just need grab the date, decide based on the date which link to put up, and the user's browser will be use your logic to pick a link for the day -- assuming they've got JS active.

Whatever you can do or dream you can, begin it.
Boldness has genius, power and magic in it. Begin it now. Chroniclemaster1, Founder of Earth ChronicleA Growing History of our Planet, by our Planet, for our Planet.

Please don't leave me if someone has replied if you think you can help, your advice is extremely good.

I have found a setTimeout won't work in this case for the reason I mentioned above. I need a separate set timeout for a page refresh for every night at midnight, that will accomplish the same thing. So I have this (need the page refresh in the script and not in the page for important reasons).

OK, so the script is computing how long it is until midnight, and calling itself at that point?

Whatever you can do or dream you can, begin it.
Boldness has genius, power and magic in it. Begin it now. Chroniclemaster1, Founder of Earth ChronicleA Growing History of our Planet, by our Planet, for our Planet.

Part of the problem is that getTime lists the time in milliseconds since January 1, 1970. So this function is subtracting (at my current time today , 86400 - 1185165929984). So your set timeout interval is ridiculously negative and never runs (and not surprisingly throws several errors to boot). Even after I ditched the getTime, I still can't get this format to work no matter what I try.

var fromMidnight = new Date(yr,mt,dy,0,0,0,0);

Someone else may have better luck, but I could only get this to pull up correctly by calling new Date first and then setting variables from it. I think it may only be complaining about the yr because that's the first variable set. See if you can get this to work. It's based on the clock that I use.

If that works then we may have to go in a different direction. It might not be possible to grab the time in milliseconds for midnight of each day, if we can't pass a date into the function. We'll just have to find a different way to catch midnight.

Whatever you can do or dream you can, begin it.
Boldness has genius, power and magic in it. Begin it now. Chroniclemaster1, Founder of Earth ChronicleA Growing History of our Planet, by our Planet, for our Planet.

chronicle master your a gem! I'll take a look at it. Thing is I got way behind so it may be awhile before I get to it. But I'll send you something anway for all your help. But your right I found out the other code was very flawed as well. I thought someone gave me a mostly working code, but it turned out it was an example only. Jen