SitePoint Sponsor

User Tag List

Results 1 to 14 of 14

Thread: if =

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

    if =

    Hello, I have been working on this for some time and am stumped and really need help. This shouldn't be hard for those that understand javascript better than I do.

    For some reason the error message I get for this says it wants a paranthesis here right after 24: "if (tm() = 24)"
    Which makes no sense because current time results from javscript should look like this 00:00:00. The first part being the hours, second being the minutes, and last being the seconds. So I don't know why it would want a paranthesis there. But when I do put a paranthesis there just to remove the error, it doesn't refresh the page at the right time anyway.

    Here's the code: All it supposed to do is get the current time for the day. If the time for the day is midnight, it is supposed to do a setTimeout in 3 seconds after midnight.

    <script type="text/javascript">
    function tm(){
    var d = new Date()
    $hr = d.getHours()
    $mn = d.getMinutes()
    $sc = d.getSeconds()}
    if (tm() = 24:60:60)
    {setTimeout('self.location.reload(true)',3000);}
    </script>

    Please let me know what I am doing wrong, thanks very much.

  2. #2
    SitePoint Zealot
    Join Date
    Nov 2005
    Posts
    113
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    1) Why are you using $ sign for variable names? This is Javasript!
    2) Missing ending semicolon
    3) Function returned nothing, you were expecting a string
    4) 24:60:60 is invalid in 2 ways, A) Its a string, so must be quoted, B) There is no 24 hour digit, 24 is actually 0 hours
    5) Theres more flexibility if you compared each hr, mn and sc digit seperately than a rigid combined string!
    6) You used = assignment operator, should have been == comparison operator
    7) Could have put everything inside a function, no need to put the if conditional outside
    8) Flawed logic #1, to reload a page only within a one second window! My code below reloads within a 3 second window is just as ridiculous. The page will never reload as you intended.
    9) Flawed logic #2, javascript is client side, my midnight may be the servers 10PM.

    Code JavaScript:
    function tm()
    {
      var d = new Date();
      var hr = d.getHours();
      var mn = d.getMinutes();
      var sc = d.getSeconds();
     
      if ( hr == 0 && mn == 1 && sc < 4 )
      {
        setTimeout('self.location.reload(true)',3000);
      }
     
      return false;
    }
    tm();

    Sorry to sound harsh, just giving it to you straight up. Good luck tough.

  3. #3
    SitePoint Addict
    Join Date
    May 2006
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks very much, I appreciate the help and learning. I just pieced that together from javascripts I found online. This is what I have but its not refreshing the page.

    Code:
    function tm()
    {
      var d = new Date();
      var hr = d.getHours();
      var mn = d.getMinutes();
      var sc = d.getSeconds();
     
      if ( hr == 23 && mn > 59 && sc > 59 )
      {
        setTimeout('self.location.reload(true)',5000);
      }
      
      return false;
    }tm();
    I read where the the 24th hour is actually the 23rd? So if it gets past midnight it is supposed to do a page refresh in about 4 or 5 seconds. Can anyone spot why the page refresh isn't working? Thank you.

  4. #4
    SitePoint Enthusiast
    Join Date
    Aug 2006
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you can never have more than 59 minutes in an hour, or 59 seconds in a minute. both wrap to 0. 23:59:59 is the latest possible time in the time, you can't have anything greater than it.

  5. #5
    SitePoint Addict
    Join Date
    May 2006
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That makes perfect sense thank you very much. Unfortunately it isn't working.

    if ( hr == 1 && mn > 1 && sc > 1 )
    are these variables added together correctly? I believe hr 1 would be after one am.


    And the whole code thats in the page above the last head tag. Isn't it supposed to refresh based on time on our own computer? Can anyone see why this isn't working? Thank you very much.

    <script type="text/javascript">
    function tm()
    {
    var d = new Date();
    var hr = d.getHours();
    var mn = d.getMinutes();
    var sc = d.getSeconds();

    if ( hr == 1 && mn > 1 && sc > 1 )
    {
    setTimeout('self.location.reload(true)',5000);
    }

    return false;
    }tm();
    </script>
    Last edited by 1Jen; Jul 22, 2007 at 17:16.

  6. #6
    SitePoint Zealot
    Join Date
    Nov 2005
    Posts
    113
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    1Jen, I thought you wanted to do it on a page load, if you need the script to run while a user is on the page, then use setInterval, you can also use setTimeout, but it is easier in your case to use setInterval. Here is an example.

    setInterval( functionName,milliseconds );

    **removed incomplete and buggy example code please check below #11 for fixed code**

    Difference setInterval v. setTimeout
    http://javascript.about.com/library/blstvsi.htm
    (author is a member on sitepoint too!)
    Last edited by abb; Jul 23, 2007 at 02:23. Reason: removed incomplete code

  7. #7
    SitePoint Addict
    Join Date
    May 2006
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I really appreciate getting help with this, I thought getting a page refresh based on what time it is would be easy, but its a lot more work than I thought. Thanks for the great link on setTimeout etc.

    It isn't working so I thought I better set up an example scenario using this code, so that hopefully someone can tell me what I am still doing wrong. I'll put my comments below, so you can see what my thinking is when testing this code.

    Code:
    function tm()
    {
      var d = new Date();
      var hr = d.getHours();
      var mn = d.getMinutes();
      var sc = d.getSeconds();
      
      if ( hr == 20 && mn == 1 && sc < 5 ) // I want to test this code to see if the page will refresh for just after 8:00 pm so why the time is listed with these numbers here.
      {
        // self.location.reload(true)
        document.location.href="http://www.website.com/test.html#hello"; // i am using this to test, so I can see #hello if it works I put the full path to the file. 
        //clearInterval(midnight_runner); // No need for this on your live script, since it runs only once
      }
    }
     
    var chk_d = new Date();
    var chk_hr = chk_d.getHours();
    if ( chk_hr > 19 && chk_hr < 23 ) // I want to test this code between 7-12 pm pm so why these numbers are listed here. (user time)
    {
      var midnight_runner = setInterval( tm, 10000 ); // run tm() keeping 10 seconds here for testing just to see if the page refresh occurs at the time listed in the first time variables listed at hr == 20...
    }
    Am I setting this code up correctly? Because when I run it this way, no refresh happens and the word hello doesn't appear in the browser bar.
    Last edited by 1Jen; Jul 22, 2007 at 19:32.

  8. #8
    SitePoint Zealot
    Join Date
    Nov 2005
    Posts
    113
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    1Jen, you have to be very literal when scripting in general, like, run script only on 19 hours, in the first minute and not all of the first minute, only the first x seconds, reason why we need to the seconds detail? If the interval time is 1 second lets say, the page will reload once say on 19:01 as expected, then as the page reloads, if its still 19:01, the script will run again and the page will reload again, so we specify down to seconds when the page should reload.

    In my example script I made a big mistake, since the interval was 30 seconds, the sc comparison has to be at least that value. (I have corrected this on my original post.) But I admit I'm a little confused as to which is the best seconds condition there, I'm thinking it should be fine if it matches the interval time.

    **removed incomplete and buggy example code please check below #11 for fixed code**
    Last edited by abb; Jul 23, 2007 at 02:23. Reason: removed incomplete code

  9. #9
    SitePoint Addict
    Join Date
    May 2006
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks very much, nothing happening here:

    function tm()
    {
    var d = new Date();
    var hr = d.getHours();

    //var mn = d.getMinutes();
    //var sc = d.getSeconds();
    // For testing, comment out minute and sc, if the script works, add in mn and sc one by one and narrow
    //down hr.
    // add back in later, && mn == 1 && sc < 5 ) ( hr == 19 && mn == 4 && sc < 5 )

    if ( hr > 20)
    {self.location.reload(true);
    alert(hr +':');}

    var chk_d = new Date();
    var chk_hr = chk_d.getHours();
    if ( chk_hr > 18 && chk_hr < 23 ) // run between 8PM and midnight only (user time)
    {

    var midnight_runner = setInterval( tm, 5000 ); // run tm() every 5 seconds when testing
    //clearInterval(midnight_runner); // No need for this on your live script, since it runs only once
    }
    }
    Or here:

    <script type="text/javascript">
    function tm()
    {
    var d = new Date();
    var hr = d.getHours();

    //var mn = d.getMinutes();
    //var sc = d.getSeconds();
    // For testing, comment out minute and sc, if the script works, add in mn and sc one by one and narrow
    //down hr.
    //to add back in later, && mn == 1 && sc < 5 ) ( hr == 19 && mn == 4 && sc < 5 )

    if ( hr > 20)
    {setInterval('self.location.reload(true)',5000);
    alert(hr +':');}}

    </script>
    Shouldn't I be getting an alert on the second one?

  10. #10
    SitePoint Addict
    Join Date
    May 2006
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    stay tuned
    Last edited by 1Jen; Jul 23, 2007 at 02:28.

  11. #11
    SitePoint Zealot
    Join Date
    Nov 2005
    Posts
    113
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Edit: Glad you got it working! I posted this before reading your reply sigh. anyways here my new code ...

    Have adjusted the script, it should work as is this time, just copy paste the whole thing into the <head> part of your page, remove the comments if you want later on. Read it as it is, don't reinterpret it.
    Code JavaScript:
    // This is a stopper, it will check if string "midnight_passing" in the URL as a query string
    // If exist, don't run this code anymore, our job is done. Otherwise, we have work to do.
    if ( window.top.location.search.substring(1).indexOf('midnight_passing') == -1 )
    {
      // PART 1: Define the "precise" time checker and reloader
      function midnight_reloader()
      {
        var d = new Date();
        // Some hardcoded values to simulate time, only for testing
        var hr = 0;//d.getHours();
        var mn = 1;//d.getMinutes();
        var sc = 10;//d.getSeconds();
     
        // change this to fit your needs or just leave it
        // This will run on 0 hours, on the first minute between 0 and 10 seconds 
        if ( hr == 0 && mn == 1 && sc < 11 )
        {
          // If its now 00:01:10, this script will run
          // On your URL append "?midnight_passing" to the back of it like here
          // we need this to check if the page was reloaded by this script
          document.location.href="test.html?midnight_passing";
        }
      }
     
      // PART 2 : Setup a timer and a loose time checker
      // Setup setInterval() callback timer, because we do not need this running  
      // anytime except near midnight, otherwise its kind of wasteful
      var chk_d = new Date();
      // simulating time, for testing
      var chk_hr = 0;//chk_d.getHours();
      var chk_mn = 1;//chk_d.getMinutes();
     
      // If user is here between 11:45PM and 12:02AM, then we need to prepare to reload the page
      // this is not precise, we depend on midnight_reloader() instead to check the time further
      // Remember that this part runs only on page load, once only.
      if ( (chk_hr == 23 && chk_mn > 45) || (chk_hr == 0 && chk_mn < 2) )
      {
          var midnight_timer = setInterval( midnight_reloader, 10000 );
          // But setInterval, will run midnight_reloader() every 10 seconds if all conditions have been met
          // So our midnight_reloader() will do the work of checking if its midnight every 10 seconds
      }
    }

    Know that without the setInterval() callback, this script will run only once when the page loads, setInterval() will call the function in its first argument again and again, the 2nd argument is the gap between each call. Remember otherwise, the script will run only once, so there will be little chance of the page auto reloading as you wished.
    Last edited by abb; Jul 23, 2007 at 17:40. Reason: Posted too late, didn't see 1Jen already fixed it ... and corrected more typos #%@

  12. #12
    SitePoint Addict
    Join Date
    May 2006
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK, I'm on the right track now. Things are happening, thanks a lot! I'll send you something. Solved
    Last edited by 1Jen; Jul 23, 2007 at 09:23.

  13. #13
    SitePoint Zealot
    Join Date
    Nov 2005
    Posts
    113
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dude, no need to send me anything!! Your thanks is enough, and besides, I gain much from sitepoint forums, even for your example I am learning stuff there, just spreading some love back, remember the currency in use here is knowledge ( hehee or something like that, what do I know.) And you don't have to pay it back to me directly.

  14. #14
    SitePoint Addict
    Join Date
    May 2006
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What a great guy!


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
  •