Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New to the CF scene
    Join Date
    Oct 2009
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Load code based on minute

    I need a code that will load multiple codes based on what minute it is.

    Example:
    If it's 12:31 it will load code A
    If it's 12:32 it will load code B
    (Lets just say there are only two codes)
    If it's 12:33 it will load code A
    If it's 12:34 it will load code B
    etc.

    I use to have this in a file somewhere, but over the course of the past few years I've misplaced it an am unable to find it.

    It would be much appreciated if I could get some help with this.

  • #2
    New Coder
    Join Date
    Mar 2009
    Posts
    74
    Thanks
    3
    Thanked 3 Times in 3 Posts
    You can just use the Date object to get the current time:

    Code:
    var mydate=new Date()
    var mytime=mydate.getMinutes()
    if(mytime==31)
    {
    ...
    }
    else if(mytime==32)
    {
    ...
    }
    getMinutes returns a number between 0-59 of the current number of minutes in the hour. So if it's 12:32 it will return 32.

  • #3
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,969
    Thanks
    56
    Thanked 557 Times in 554 Posts
    but let's say there are only 2 scripts... you'd save yourself some work by using the modulus

    Code:
    var mydate=new Date()
    var mytime=mydate.getMinutes()
    if (mytime%2=0) {
    //load script A
    } else {
    //load script B
    }

  • #4
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    @xelawho can you explain that to me a little bit? I've been trying to understand the modulus for some time now, and it seems beyond my understanding... (but it's in a few borrowed scripts I use )

  • #5
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,969
    Thanks
    56
    Thanked 557 Times in 554 Posts
    damn... I was hoping to slide that one by...

    far as I can tell the modulus is for checking if a number divides "cleanly".

    it returns the remainder, so in this case it checks if the minute is even or odd (by dividing by two). if it returns 0 obviously it was even, so load script A, if it returns anything else, it was odd, so load script B

    hey, check it out - maybe I do understand it...

  • #6
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    An odd/even check... I like it!

    Edit:
    I've already used it to replace
    Code:
    j=1;
    for(i=0 ; i<10 ; i++){
     j=i*-1;                         //which I thought was totally clever of me...
     color=j==1?'white':'#AAAAAA';
     }
    with
    Code:
    for(i=0 ; i<10 ; i++){color=i%2?'white':'#AAAAAA';}
    Awesome!
    Last edited by blaze4218; 09-23-2011 at 10:19 PM.

  • #7
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,969
    Thanks
    56
    Thanked 557 Times in 554 Posts
    cool, huh?

    now if only someone could explain why it works the same with either single or double =

    Code:
    mytime=2;
    yourtime=3;
    
    if(mytime%2!=0) {
    alert("mytime is odd")
    } else {
    alert("mytime is even")
    }
    
    if(yourtime%2!==0) {
    alert("yourtime is odd")
    } else {
    alert("yourtime is even")
    }

  • #8
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,087
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Lightbulb

    Quote Originally Posted by xelawho View Post
    cool, huh?

    now if only someone could explain why it works the same with either single or double =

    Code:
    mytime=2;
    yourtime=3;
    
    if(mytime%2!=0) {
    alert("mytime is odd")
    } else {
    alert("mytime is even")
    }
    
    if(yourtime%2!==0) {
    alert("yourtime is odd")
    } else {
    alert("yourtime is even")
    }
    See the first 4 operators of this site:
    http://www.javascriptkit.com/jsref/c...perators.shtml

  • #9
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,969
    Thanks
    56
    Thanked 557 Times in 554 Posts
    aaaaaaaaaaaaaaaaaaaaaah, snap.

    so != is to !==

    as == is to === ?

    I never knew that. Thanks, jmrker!

  • #10
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,087
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Thumbs up

    Quote Originally Posted by xelawho View Post
    aaaaaaaaaaaaaaaaaaaaaah, snap.

    so != is to !==

    as == is to === ?

    I never knew that. Thanks, jmrker!
    Yes, but in a very "strick" sense.

  • #11
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,037
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    You should note that the code to be loaded will depend on the time as shown by the user's computer, which may well be inaccurate.

    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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