SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Addict
    Join Date
    May 2006
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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.

    Code:
    function links() {
    a( 196, "http://www.google.com","testlink"); 
    a( 197, "http://www.google.com","testlink2"); 
    a( 198, "http://www.google.com","testlink3"); 
    }
    bad news, don't follow this.
    }
    Last edited by 1Jen; Jul 23, 2007 at 23:37.

  2. #2
    SitePoint Addict
    Join Date
    May 2006
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does anyone know about setTimeout? Is this not working because I'm asking it to set a Timeout on the wrong thing? Please let me know, thanks a lot.

  3. #3
    SitePoint Guru Chroniclemaster1's Avatar
    Join Date
    Jun 2007
    Location
    San Diego, CA
    Posts
    784
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi 1Jen!

    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 Chronicle
    A Growing History of our Planet, by our Planet, for our Planet.

  4. #4
    SitePoint Addict
    Join Date
    May 2006
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  5. #5
    SitePoint Addict
    Join Date
    May 2006
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.
    Last edited by 1Jen; Jul 18, 2007 at 01:23.

  6. #6
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just test the above script by making the timer faster, say 2-3 seconds. Also, I don't see where links is called the first time. And... what is nowDoy? What calculation are you using for that?

    And one more thing, you don't need to use quotes when you use the setTimeout function. Just pass the function
    Code:
    window.setTimeout(links, 3600000);

  7. #7
    SitePoint Guru Chroniclemaster1's Avatar
    Join Date
    Jun 2007
    Location
    San Diego, CA
    Posts
    784
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok here are my, literally 2 cents...

    1. here's the format of my setTimeout

    Code:
     dateDate=setTimeout("ecClock()",1000);

    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 Chronicle
    A Growing History of our Planet, by our Planet, for our Planet.

  8. #8
    SitePoint Addict
    Join Date
    May 2006
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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).

    From the .js script:

    function tii(){window.setTimeout("location.href='http://www.website.com/test.html'")}

    function ti(){with(new Date){
    yr = getFullYear(); // the year
    mt = getMonth()+1; // the month
    dy = getDate(); // todays day number}
    fromMidnight = new Date(yr,mt,dy,0,0,0,0).getTime();
    toMidnight = 86400000 - fromMidnight;
    setTimeout('tii()',toMidnight);}}
    from the body of the page: Including it with another function so I can have more than one script on my page.

    <script type="text/javascript">
    window.onload=function(){afunction();ti();}</script>
    Can anyone see why this isn't refreshing my page at midnight? Thanks very much.

  9. #9
    SitePoint Guru Chroniclemaster1's Avatar
    Join Date
    Jun 2007
    Location
    San Diego, CA
    Posts
    784
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    do you have a different link for each day of the year?

    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 Chronicle
    A Growing History of our Planet, by our Planet, for our Planet.

  10. #10
    SitePoint Addict
    Join Date
    May 2006
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've done the best with this one, with this the only error I have left is that full year is not defined. Can't spot the problem. Thanks.

    function ti(){
    var yr = getFullYear(); // the year
    var mt = getMonth()+1; // the month
    var dy = getDate(); // todays day number
    fromMidnight = new Date(yr,mt,dy,0,0,0,0).getTime();
    toMidnight = 86400000 - fromMidnight;
    setTimeout('function(){self.location.reload(true)}',toMidnight);}

    Seems like it would need to change on the new day, not a midnight, for a new day page refresh? Not sure which way is easiest.
    Last edited by 1Jen; Jul 20, 2007 at 23:56.

  11. #11
    SitePoint Guru Chroniclemaster1's Avatar
    Join Date
    Jun 2007
    Location
    San Diego, CA
    Posts
    784
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, I think I've got a better handle on this.

    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.

    Code:
    // Call the date object and name variables for it's needed components
    var dateDate = new Date();
     
    var intYear = dateDate.getFullYear();
    var intMonth = dateDate.getMonth();
    var intDate = dateDate.getDate();
    var intDay = dateDate.getDay();
    var intHours = dateDate.getHours();
    var intMinutes = dateDate.getMinutes();
    var intSeconds = dateDate.getSeconds();
    var time = dateDate.getTime();
     
     
     
    document.write("The year is " + intYear + ". <br> The month is " + intMonth + ". <br> The date is " + intDate + ". <br> The day is " + intDay + ". <br> The hour is " + intHours + ". <br> The minute is " + intMinutes + ". <br> The second is " + intSeconds + ". <br> The time is " + time + " milliseconds.");
    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 Chronicle
    A Growing History of our Planet, by our Planet, for our Planet.

  12. #12
    SitePoint Addict
    Join Date
    May 2006
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •