You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
2.0 KiB

2 years ago
  1. $(function(){
  2. function timer(settings){
  3. var config = {
  4. endDate: '2019-05-19 09:00',
  5. timeZone: 'Europe/Dublin',
  6. hours: $('#hours'),
  7. minutes: $('#minutes'),
  8. seconds: $('#seconds'),
  9. newSubMessage: 'and should be back online in a few minutes...'
  10. };
  11. function prependZero(number){
  12. return number < 10 ? '0' + number : number;
  13. }
  14. $.extend(true, config, settings || {});
  15. var currentTime = moment();
  16. var endDate = moment.tz(config.endDate, config.timeZone);
  17. var diffTime = endDate.valueOf() - currentTime.valueOf();
  18. var duration = moment.duration(diffTime, 'milliseconds');
  19. var days = duration.days();
  20. var interval = 1000;
  21. var subMessage = $('.sub-message');
  22. var clock = $('.clock');
  23. if(diffTime < 0){
  24. endEvent(subMessage, config.newSubMessage, clock);
  25. return;
  26. }
  27. if(days > 0){
  28. $('#days').text(prependZero(days));
  29. $('.days').css('display', 'inline-block');
  30. }
  31. var intervalID = setInterval(function(){
  32. duration = moment.duration(duration - interval, 'milliseconds');
  33. var hours = duration.hours(),
  34. minutes = duration.minutes(),
  35. seconds = duration.seconds();
  36. days = duration.days();
  37. if(hours <= 0 && minutes <= 0 && seconds <= 0 && days <= 0){
  38. clearInterval(intervalID);
  39. endEvent(subMessage, config.newSubMessage, clock);
  40. window.location.reload();
  41. }
  42. if(days === 0){
  43. $('.days').hide();
  44. }
  45. $('#days').text(prependZero(days));
  46. config.hours.text(prependZero(hours));
  47. config.minutes.text(prependZero(minutes));
  48. config.seconds.text(prependZero(seconds));
  49. }, interval);
  50. }
  51. function endEvent($el, newText, hideEl){
  52. $el.text(newText);
  53. hideEl.hide();
  54. }
  55. timer();
  56. });