...

View Full Version : Load code based on minute



SSlimJim
09-23-2011, 09:53 PM
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.

Raphael
09-23-2011, 10:08 PM
You can just use the Date object to get the current time:



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.

xelawho
09-23-2011, 10:47 PM
but let's say there are only 2 scripts... you'd save yourself some work by using the modulus



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

blaze4218
09-23-2011, 10:51 PM
@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 :) )

xelawho
09-23-2011, 10:58 PM
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... :D

blaze4218
09-23-2011, 11:04 PM
An odd/even check... I like it!

Edit:
I've already used it to replace

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

for(i=0 ; i<10 ; i++){color=i%2?'white':'#AAAAAA';}
Awesome!

xelawho
09-23-2011, 11:15 PM
cool, huh?

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



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")
}

jmrker
09-24-2011, 02:51 AM
cool, huh?

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



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/comparison_operators.shtml
:thumbsup:

xelawho
09-24-2011, 03:08 AM
aaaaaaaaaaaaaaaaaaaaaah, snap.

so != is to !==

as == is to === ?

I never knew that. Thanks, jmrker!

jmrker
09-24-2011, 03:17 AM
aaaaaaaaaaaaaaaaaaaaaah, snap.

so != is to !==

as == is to === ?

I never knew that. Thanks, jmrker!

Yes, but in a very "strick" :D sense.

Philip M
09-24-2011, 08:23 AM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum