PluginProbe
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant / 1.7
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant v1.7
2.3.1 2.3.0 2.2.8 2.2.7 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.11.0 1.11.1 1.11.2 1.6 1.7 1.8 1.8.1 1.8.2 1.8.3 1.9.0 1.9.1 1.9.10 1.9.11 1.9.12 All 59 releases
merchant / assets / js / modules / countdown-timer / countdown-timer.js

countdown-timer.js in Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant 1.7, at assets/js/modules/countdown-timer/countdown-timer.js

102 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 "use strict";
2
3 ;
4 (function ($, window, document, undefined) {
5 'use strict';
6
7 var countdownTimer, countdownTimerDate, countdownMinExpiration, countdownMaxExpiration;
8 var countdownTimerId = 'merchant-countdown-timer';
9 var countdownTimerCookie = 'merchant_countdown_timer_date';
10 var countdownTimerCoolOffCookie = 'merchant_countdown_timer_cool_off_date';
11 var setCookie = function setCookie(name, value, expirationDays) {
12 var date = new Date();
13 date.setTime(date.getTime() + expirationDays * 24 * 60 * 60 * 1000); // Calculate expiration date
14
15 var expires = "expires=" + date.toUTCString();
16 document.cookie = name + "=" + value + ";" + expires + ";path=/";
17 };
18 var getCookie = function getCookie(name) {
19 var cookieArr = document.cookie.split(';');
20 for (var i = 0; i < cookieArr.length; i++) {
21 var cookiePair = cookieArr[i].split('=');
22 var cookieName = cookiePair[0].trim();
23 var cookieValue = cookiePair[1].trim();
24 if (cookieName === name) {
25 return decodeURIComponent(cookieValue);
26 }
27 }
28 return null;
29 };
30 var deleteCookie = function deleteCookie(name) {
31 document.cookie = name + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
32 };
33 var updateCountdown = function updateCountdown() {
34 var now = new Date().getTime();
35 var minExpirationTime = countdownMinExpiration.getTime();
36 var maxExpirationTime = countdownMaxExpiration.getTime();
37 if (now < minExpirationTime) {
38 // The current time is before the minimum expiration deadline
39 displayCountdown(maxExpirationTime - now);
40 } else if (now > maxExpirationTime) {
41 // The current time is after the maximum expiration deadline
42 destroyCountDown();
43 } else {
44 // The current time is between the minimum and maximum expiration deadlines
45 destroyCountDown();
46 }
47 };
48 var displayCountdown = function displayCountdown(timeLeft) {
49 var days = Math.floor(timeLeft / (1000 * 60 * 60 * 24));
50 var hours = Math.floor(timeLeft % (1000 * 60 * 60 * 24) / (1000 * 60 * 60)).toString().padStart(2, '0');
51 var minutes = Math.floor(timeLeft % (1000 * 60 * 60) / (1000 * 60)).toString().padStart(2, '0');
52 var seconds = Math.floor(timeLeft % (1000 * 60) / 1000).toString().padStart(2, '0');
53 document.getElementById(countdownTimerId).innerHTML = "".concat(days, " days ").concat(hours, ":").concat(minutes, ":").concat(seconds);
54 if (timeLeft > 0) {
55 setTimeout(updateCountdown, 1000);
56 }
57 };
58 var destroyCountDown = function destroyCountDown() {
59 displayCountdown(0);
60 deleteCookie(countdownTimerCookie);
61 setCoolOffDate();
62 countdownTimer.hide();
63 };
64 var setCoolOffDate = function setCoolOffDate() {
65 if (!getCookie(countdownTimerCoolOffCookie)) {
66 var coolOffDate = new Date();
67 coolOffDate.setMinutes(coolOffDate.getMinutes() + parseInt(countdownTimer.attr('data-cop')));
68 setCookie(countdownTimerCoolOffCookie, coolOffDate, 1);
69 }
70 };
71 $(document).ready(function () {
72 countdownTimer = $('.merchant-countdown-timer');
73 if (countdownTimer.length) {
74 var maxExpirationHours = parseInt(countdownTimer.attr('data-max'));
75 var minExpirationHours = maxExpirationHours - parseInt(countdownTimer.attr('data-min'));
76 var startCountdown = true;
77 if (getCookie(countdownTimerCoolOffCookie)) {
78 var coolOffPeriodDate = new Date(getCookie(countdownTimerCoolOffCookie));
79 var now = new Date().getTime();
80 if (now > coolOffPeriodDate) {
81 deleteCookie(countdownTimerCoolOffCookie);
82 } else {
83 startCountdown = false;
84 }
85 }
86 if (startCountdown) {
87 countdownTimer.show();
88 if (!getCookie(countdownTimerCookie)) {
89 countdownTimerDate = new Date();
90 setCookie(countdownTimerCookie, countdownTimerDate, 9);
91 } else {
92 countdownTimerDate = new Date(getCookie(countdownTimerCookie));
93 }
94 countdownMaxExpiration = new Date(countdownTimerDate.getTime());
95 countdownMaxExpiration.setHours(countdownMaxExpiration.getHours() + maxExpirationHours);
96 countdownMinExpiration = new Date(countdownTimerDate.getTime());
97 countdownMinExpiration.setHours(countdownMinExpiration.getHours() + minExpirationHours);
98 updateCountdown();
99 }
100 }
101 });
102 })(jQuery, window, document);