Page 3 of 6 FirstFirst 12345 ... LastLast
Results 21 to 30 of 60

Thread: JavaScript Validation?

  1. #21
    Join Date
    Feb 2007
    Location
    🌎
    Posts
    528
    Thanks
    10
    Thanked 10 Times in 10 Posts
    Blog Entries
    2

    Default

    And I take it that if it's 10:21, it writes '4,' not 'in 4 minutes.' Is that right?

  2. #22
    Join Date
    Feb 2007
    Location
    🌎
    Posts
    528
    Thanks
    10
    Thanked 10 Times in 10 Posts
    Blog Entries
    2

    Default

    Aye. Everything was thinking that I wanted the page to refrech the page every nanosecond . Does that refresh every minute? Or can I even do it in the script? Do I need the <meta http-equiv="refresh"> tag?

  3. #23
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    var leaves = (function(x) { var v = x &#37; 5; return (v === 0 ? 0 : 5) - v; })((new Date()).getMinutes());
    Sorry, that's kind of advanced... it creates an anonymous function (one without a name), then calls it with the current minutes as an argument. It's equivalent to:
    Code:
    function f(x) {
      var v = x % 5;
      if(v === 0)
        return v;
      else
        return 5 - v;
    }
    
    var d = new Date();
    var leaves = f(d.getMinutes());
    ... but obviously much shorter.
    Code:
    window.location.href = (leaves === 0 ? "shuttle.html" : "index.html");
    This is an example of the tertiary operator.
    Code:
    var a = b ? c : d;
    is equivalent to:
    Code:
    var a;
    if(b)
      a = c;
    else
      a = d;
    Is there any way to make that update every minute...or does it already?
    No, it doesn't. Since the user is redirected immediately, there's no point... oh. Now I see what you were attempting with location.href = "index.html". Don't do this. Instead, try something like:
    Code:
    <p>Welcome to SGB Airport.  Next shuttle leaves in <span id="nextshuttle">?</span>.</p>
    
    <script type="text/javascript">
      var leaves = (function() { var v = (new Date()).getMinutes() % 5; return (v === 0 ? 0 : 5) - v; })();
    
      function updateMinutes() {
        var l = leaves();
        document.getElementById("nextshuttle").firstChild.nodeValue = l + (l === 1 ? " minute" : " minutes");
      }
      setInterval(updateMinutes, 60000);
    </script>
    I never learned what an array was, all I learned was if...else and switch.
    Arrays are vital in Javascript.
    And I take it that if it's 10:21, it writes '4,' not 'in 4 minutes.' Is that right?
    No. What lead you to that conclusion?
    Everything was thinking that I wanted the page to refrech the page every nanosecond . Does that refresh every minute? Or can I even do it in the script? Do I need the <meta http-equiv="refresh"> tag?
    Ugh! The original doesn't, but the one above will update the time every minute. You don't need <meta>.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #24
    Join Date
    Feb 2007
    Location
    🌎
    Posts
    528
    Thanks
    10
    Thanked 10 Times in 10 Posts
    Blog Entries
    2

    Default

    Yeah. I think the cause of the refreshing was
    Code:
    else {
    window.location="index.html"
    }
    It should have been
    Code:
    else {
    }

  5. #25
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Yes, I said this. It should, in fact, have been:
    Code:
    
    
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #26
    Join Date
    Feb 2007
    Location
    🌎
    Posts
    528
    Thanks
    10
    Thanked 10 Times in 10 Posts
    Blog Entries
    2

    Default

    Except now I get 'Welcome to SGB Airport. Next shuttle leaves in ?. ..'

  7. #27
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Not if you wait a minute You want to call it once first:
    Code:
    <p>Welcome to SGB Airport.  Next shuttle leaves in <span id="nextshuttle">?</span>.</p>
    
    <script type="text/javascript">
      var leaves = (function() { var v = (new Date()).getMinutes() &#37; 5; return (v === 0 ? 0 : 5) - v; })();
    
      function updateMinutes() {
        var l = leaves();
        document.getElementById("nextshuttle").firstChild.nodeValue = l + (l === 1 ? " minute" : " minutes");
      }
    
      setInterval(updateMinutes, 60000);
      updateMinutes();
    </script>
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  8. #28
    Join Date
    Feb 2007
    Location
    🌎
    Posts
    528
    Thanks
    10
    Thanked 10 Times in 10 Posts
    Blog Entries
    2

    Default

    Wait. I think you should see this:

    I opened it in Firefox w/ Firebug ang got this message.

  9. #29
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    ... what message?

    ImageShack doesn't like me.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  10. #30
    Join Date
    Feb 2007
    Location
    🌎
    Posts
    528
    Thanks
    10
    Thanked 10 Times in 10 Posts
    Blog Entries
    2

    Default

    And it throws 4 more of those every minute.

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
  •