PluginProbe
Blockenberg — 600+ Advanced Gutenberg Blocks & AI Agent for WordPress Block Editor / 2.0.7
Blockenberg — 600+ Advanced Gutenberg Blocks & AI Agent for WordPress Block Editor v2.0.7
2.0.13 2.0.12 2.0.11 2.0.10 2.0.9 trunk 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8
blockenberg / blocks / cta-countdown / frontend.js

frontend.js in Blockenberg — 600+ Advanced Gutenberg Blocks & AI Agent for WordPress Block Editor 2.0.7, at blocks/cta-countdown/frontend.js

54 lines 2.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 (function () {
2 function padZ(n) { return n < 10 ? '0' + n : String(n); }
3
4 function getEndTime(wrap) {
5 var isRelative = wrap.dataset.relative === '1';
6 var endTime;
7 if (isRelative) {
8 var days = parseInt(wrap.dataset.days || '3', 10);
9 // Store in sessionStorage so it stays stable on page reload
10 var storageKey = 'bkbg-ctc-' + (wrap.closest('[id]') || { id: 'x' }).id + '-end';
11 var stored = sessionStorage.getItem(storageKey);
12 if (stored) {
13 endTime = new Date(parseInt(stored, 10));
14 } else {
15 endTime = new Date(Date.now() + days * 86400000);
16 sessionStorage.setItem(storageKey, endTime.getTime().toString());
17 }
18 } else {
19 endTime = new Date(wrap.dataset.end || '');
20 }
21 return isNaN(endTime.getTime()) ? null : endTime;
22 }
23
24 function tick(wrap, units, endTime) {
25 var now = Date.now();
26 var diff = Math.max(0, endTime.getTime() - now);
27
28 var totalSecs = Math.floor(diff / 1000);
29 var days = Math.floor(totalSecs / 86400);
30 var hours = Math.floor((totalSecs % 86400) / 3600);
31 var minutes = Math.floor((totalSecs % 3600) / 60);
32 var seconds = totalSecs % 60;
33
34 var vals = { days: padZ(days), hours: padZ(hours), minutes: padZ(minutes), seconds: padZ(seconds) };
35
36 units.forEach(function (u) {
37 var numEl = u.querySelector('.bkbg-ctc-unit-num');
38 if (numEl) numEl.textContent = vals[u.dataset.unit] || '00';
39 });
40
41 if (diff > 0) {
42 setTimeout(function () { tick(wrap, units, endTime); }, 1000);
43 }
44 }
45
46 document.querySelectorAll('.bkbg-ctc-wrap').forEach(function (wrap) {
47 var endTime = getEndTime(wrap);
48 if (!endTime) return;
49 var units = Array.from(wrap.querySelectorAll('.bkbg-ctc-unit[data-unit]'));
50 if (!units.length) return;
51 tick(wrap, units, endTime);
52 });
53 })();
54