Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

1. ## 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.

• 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.

• 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) {
} else {
}```

• @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 )

• 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...

• 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!

• 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) {
} else {
}

if(yourtime%2!==0) {
} else {
}```

• Originally Posted by xelawho
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) {
} else {
}

if(yourtime%2!==0) {
} else {
}```
See the first 4 operators of this site:
http://www.javascriptkit.com/jsref/c...perators.shtml

• aaaaaaaaaaaaaaaaaaaaaah, snap.

so != is to !==

as == is to === ?

I never knew that. Thanks, jmrker!

• Originally Posted by xelawho
aaaaaaaaaaaaaaaaaaaaaah, snap.

so != is to !==

as == is to === ?

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

• 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.

•

#### Posting Permissions

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