Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save charliepark/93248 to your computer and use it in GitHub Desktop.
Save charliepark/93248 to your computer and use it in GitHub Desktop.
/*
A way to set your z-index to dynamically update, based on the current time.
When you click on the object ('.element'), the javascript grabs the current time,
concatenates the hours, minutes, and seconds, and sets that number as the z-index for the object.
*/
$('.element').mousedown(function(event) {
var time = new Date();
var hours = time.getHours();
if (hours.toString().length == 1) { hours = "0" + '' + hours }
var minutes = time.getMinutes();
if (minutes.toString().length == 1) { minutes = "0" + '' + minutes }
var seconds = time.getSeconds();
if (seconds.toString().length == 1) { seconds = '0' + '' + seconds }
var zindex = hours + '' + minutes + '' + seconds;
$(this).css('z-index',zindex);
});
@Jawfin
Copy link

Jawfin commented Jun 28, 2019

I know this is 10 years old, but I am only posting in case someone else finds this as a solution for them - there's a couple of bugs here. getMinutes() is missing, 0's are dropped so it messes up the ordering, and this returns a string, not an integer. I propose this: -
var zindex = parseInt(time.getHours() + '' + ("0" + time.getMinutes()).slice(-2) + ("0" + time.getSeconds()).slice(-2));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment