Bespoke greeting based on time of day

Nov 05, 2013


Myself and and a colleague were chatting about how we could use Javascript to improve the user experience.  So, after taking a couple of the sample codes available on the forums (thanks to all that contribute by the way!) and tweaking them slightly, we came up with a nice way to tailor the greeting the user recieves.

So, when a user launches the module from an LMS, it will recognise the time of day and respond with the appropriate Good morning/afternoon/evening.  When coupled with the retrieve username code, now tweaked to capitalize the first letter, it does give a nice little touch.

Here you go...feel free to use

This part pulls the name and then capitalizes the first letter of first name

var player = GetPlayer();
var myName = lmsAPI.GetStudentName();
var array = myName.split(',');
var str = array[1];
var newName = str.slice(0,1).toUpperCase() + str.slice(1);
player.SetVar("newName", newName);

This second part then tailors the greeting

var player = GetPlayer();
var currentTime = new Date();
 var hours = currentTime.getHours();

if (hours < 12)
 player.SetVar("Greeting","Good morning")
 if (hours >= 12 && hours <= 16)
 player.SetVar("Greeting","Good afternoon")
 if (hours > 17)
 player.SetVar("Greeting","Good evening")

You need two variables set up...

Greeting - text variable with default text left blank

newName - text variable with default text left blank

The username part is LMS dependent, so won't work if web hosted, but the greeting component is triggered by the system time of the device playing the course, so does work when webhosted.


6 Replies

This discussion is closed. You can start a new discussion or contact Articulate Support.