Created
August 4, 2016 07:48
-
-
Save neko-fire/784bbf9241b6617fc739c34d0a9ad65e to your computer and use it in GitHub Desktop.
countdown & bg changer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(function () { | |
var main = []; | |
var colors = ['#19ba5a', '#3c82f6', '#e93750', '#29353a', '#9449b3', '#ffdb00']; | |
var interval_time = 5000; | |
$('.main').each(function () { | |
main.push($(this).prop('id')); | |
}); | |
var body = $('body'); | |
function randomNumber (max) { | |
return Math.floor(Math.random() * max); | |
} | |
var count = randomNumber(main.length) | |
// set body class and color the first time | |
body.removeClass().addClass(main[count]); | |
var colorCount = randomNumber(colors.length); | |
body.css('background-color', colors[colorCount]); | |
function changeMain () { | |
count++; | |
if (count == main.length) { | |
count = 0; | |
} | |
var current = body.prop('class'); | |
body.removeClass().addClass(current + ' ' + main[count]); | |
var newColor; | |
do { | |
newColor = randomNumber(colors.length) | |
} while (newColor == colorCount); | |
colorCount = newColor; | |
body.css('background-color', colors[colorCount]); | |
setTimeout(function () { | |
body.removeClass(current) | |
}, 500); | |
} | |
setInterval(function () { | |
changeMain(); | |
}, interval_time); | |
var one_day = 60 * 60 * 24; | |
var one_hour = 60 * 60; | |
var one_minute = 60; | |
var one_second = 1; | |
function countDown () { | |
// time difference | |
var difference = from - to; | |
// get days | |
var days = Math.floor(difference / one_day); | |
// get hours | |
var hours = Math.floor((difference % one_day) / one_hour); | |
if (hours < 10) { | |
hours = '0' + hours; | |
} | |
// get minutes | |
var minutes = Math.floor((difference % one_hour) / one_minute); | |
if (minutes < 10) { | |
minutes = '0' + minutes; | |
} | |
// get minutes | |
var seconds = difference % one_minute; | |
if (seconds < 10) { | |
seconds = '0' + seconds; | |
} | |
var line1 = '<strong>' + days + '</strong> days'; | |
var line2 = '<strong>' + hours + '</strong>:<strong>' + minutes + '</strong>:<strong>' + seconds + '</strong>'; | |
$('#countdown .days').html(line1); | |
$('#countdown .time').html(line2); | |
if (minutes == 0 && seconds == 0) { | |
location.reload(); | |
} | |
// count up to time | |
to++; | |
var total = from - start; | |
var part = to - start; | |
$('#bar').css('width', part / total * 100 + '%'); | |
} | |
countDown(); | |
setInterval(function () { | |
countDown(); | |
}, 1000); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment