I am writing my very first macro, a stardate clock. I have it finished except for one thing. I don’t know how setTimeout() like you would with normal JavaScript.
This is my sample code:
/*\
title: $:/Temporal_Accord/macros/stardate.js
type: application/javascript
module-type: macro
Stardate clock for Prime and Kelvin stardates.
\*/
(function() {
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
exports.name = "stardate";
exports.params = [];
/*
Run the macro. Make sure it accepts the parameters you have defined above.
*/
exports.run = function() {
try {
return /*output*/
} catch (err) {
console.error(err.stack)
return "(ERROR: " + err.message + ") ";
}
setTimeout()? /* How do I do this? */
};
})();
My full code is here, just in case:
/*\
title: $:/Temporal_Accord/macros/stardate.js
type: application/javascript
module-type: macro
Stardate calculator for Prime and Kelvin stardates.
\*/
(function() {
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
exports.name = "stardate";
exports.params = [];
/*
Run the macro. Make sure it accepts the parameters you have defined above.
*/
exports.run = function() {
var thisDate = new Date();
var year = thisDate.getFullYear();
var month = thisDate.getMonth() + 1;
var date = thisDate.getDate();
var hour = thisDate.getHours();
var mins = thisDate.getMinutes();
var secs = thisDate.getSeconds();
var isLeapYear = false;
/* Checking to see if this year is a leap year */
if ( (year % 4 === 0 && year % 100 !== 0) || (year % 4 === 0 && year % 100 === 0 && year % 400 === 0) ) {
isLeapYear = true;
}
/*
Calculating Prime timeline stardates:
Prime stardates use the format: YUUU.U (Y = year; U = unit).
Prime stardates have 1000 units per year.
The first step is to figure the year part in our Prime stardate.
Note: Star Trek: The Next Generation (TNG) stardates start at 41UUU.U.
The "4" in the stardate means the 24th century.
The "1" in the stardate means TNG season 1.
The First step is to calculate the year part of the stardate or prefix.
TNG first aired in 1987, the in-universe date starts in 2364.
It can be assumed that January 01st is equal to YY000.0, for both 1987, and 2364.
For this purpose, the stardate will calculate using our time.
Otherwise, negative numbers will result.
We need to figure out when stardate: 0000.0 was, this part does not require JavaScript.
1987 (the year TNG first aired) - 41 (the start of TNG season 1) = 1946.
January 01st, 1946 = stardate: 0000.0.
The easy part getting the prefix or the year part of the stardate:
*/
var sdPre = year - 1946;
/*
Now the hard part. finding the suffix of the units.
First, we need to find out how many units are in a day.
This is why we needed to see if this was a leap year or not.
We simply divide 1000 by 365 for non-leap years and 366 for leap years.
*/
var unitPerDay = 1000 / 365;
if (isLeapYear == true) { unitPerDay = 1000 / 366 }
/*
For the second part, we need to figure out how many days have passed so far this year.
In other words, we need to figure out what is the Julian date.
This will come in handy when need to figure out the Kelvin Timeline stardates.
We can start with today's date.
*/
var julian = date;
/*
Of course, this will give us this month. Now, we to add all previous months to the count.
*/
if (month >= 2) { julian = julian + 31 } /* add January */
if (month >= 3) {
julian = julian + 28 /* add February */
if (isLeapYear == true) {
julian = julian + 1 /* add the leap day if necissary */
}
}
if (month >= 4) { julian = julian + 31 } /* add March */
if (month >= 5) { julian = julian + 30 } /* add April */
if (month >= 6) { julian = julian + 31 } /* add May */
if (month >= 7) { julian = julian + 30 } /* add June */
if (month >= 8) { julian = julian + 31 } /* add July */
if (month >= 9) { julian = julian + 31 } /* add August */
if (month >= 10) { julian = julian + 30 } /* add September */
if (month >= 11) { julian = julian + 31 } /* add October */
if (month >= 12) { julian = julian + 30 } /* add November */
/*
It is not necessary to calculate December because we calculate the current month first.
Now that we know how many days have elapsed this year, we can multiply the Julian date
by stardate units per day.
But, first, we must subtract 1 from the Julian date.
Because we need January 1st to start at 0.0;
If we don't subtract we will start January 1st at 2.73972602739726 (for non-leap years);
*/
julian--;
/* Then multiply this by units per day. */
var sdSuf = julian * unitPerDay; /* Our day starts here at 12:00:00 am */
/*
Now that we have the starting point for our day, we need to figure out where we
are in the current day.
We know how many stardate units are in a minute.
There are 1,440 minutes in a day.
We simply divide units per day by 1,440
*/
var unitPerMin = unitPerDay / 1440;
/*
Now we need to know how many minutes passed per day.
This will be so much easier since JavaScript starts hours at 0, like military time.
Because we can calculate hours * 60 minutes + minutes in the current hour.
*/
var minsThisDay = hour * 60 + mins;
/* Multiply this by stardate units per minute. */
var unitThisDay = minsThisDay * unitPerMin;
/* Add this to the currend stardate suffix part */
sdSuf = sdSuf + unitThisDay;
/* need to round to the nearest tenth */
sdSuf = sdSuf.toFixed(1)
try {
return year + "-" + month + "-" + date + " @ " + hour + ":" + mins + ":" + secs + " leap year: " + isLeapYear + "<br>Stardate: " + sdPre + "" + sdSuf + "<br>Stardate units per day: " + unitPerDay + "<br>Julian Date: " + julian + "<br>Minutes this day: " + minsThisDay;
} catch (err) {
console.error(err.stack)
return "(ERROR: " + err.message + ") ";
}
};
})();