Skip to content

Commit 7a8f911

Browse files
authored
Update code-test.js
1 parent 2c1fbde commit 7a8f911

File tree

1 file changed

+37
-34
lines changed

1 file changed

+37
-34
lines changed

code-test.js

+37-34
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,38 @@
11
/* -------- AFFICHAGE DU CODE DE PROMOTION À L'INSCRIPTION -------- */
22
document.addEventListener('DOMContentLoaded', function() {
3-
initializeCouponTimer();
4-
});
5-
6-
function initializeCouponTimer() {
7-
let memberData = localStorage.getItem('_ms-mem');
8-
if (memberData) {
9-
let memberObj = JSON.parse(memberData);
10-
if (memberObj && memberObj.metaData && memberObj.metaData.coupon_name) {
11-
startCouponTimer(memberObj.metaData.coupon_name);
12-
}
13-
}
14-
3+
updatePromoCode();
154
let checkInterval = setInterval(function() {
16-
if (localStorage.getItem('timer_finished') === 'true') {
5+
let memberDataExists = localStorage.getItem('_ms-mem') !== null;
6+
let timerFinished = localStorage.getItem('timer_finished') === 'true';
7+
8+
if (timerFinished || !memberDataExists) {
179
clearInterval(checkInterval);
1810
} else {
1911
updatePromoCode();
2012
}
21-
}, 1000); // Vérifie les données du coupon toutes les secondes
22-
}
13+
}, 60000); // Vérifie les données du coupon toutes les minutes
14+
});
2315

2416
function startCouponTimer(couponName) {
25-
let startTime = localStorage.getItem('coupon_start_time');
17+
let startTime = parseInt(localStorage.getItem('coupon_start_time'), 10);
18+
let currentTime = new Date().getTime();
19+
let timerFinished = localStorage.getItem('timer_finished');
20+
2621
if (!startTime) {
27-
startTime = new Date().getTime().toString();
22+
startTime = currentTime;
2823
localStorage.setItem('coupon_start_time', startTime);
24+
} else if (timerFinished) {
25+
return;
26+
}
27+
28+
let elapsedTime = currentTime - startTime;
29+
let remainingTime = 24 * 60 * 60 * 1000 - elapsedTime;
30+
31+
if (remainingTime > 0 && couponName) {
32+
document.querySelectorAll('[data-co-offer="promo-code-wrapper"]').forEach(function(promoPopinElement) {
33+
promoPopinElement.style.display = 'block';
34+
});
2935
}
30-
startTime = parseInt(startTime, 10); // Make sure we work with the number
3136

3237
let timerInterval = setInterval(function() {
3338
updateTimer(couponName, startTime, timerInterval);
@@ -36,12 +41,17 @@ function startCouponTimer(couponName) {
3641
}
3742

3843
function updateTimer(couponName, startTime, timerInterval) {
39-
let currentTime = new Date().getTime();
40-
let elapsedTime = currentTime - startTime;
44+
let elapsedTime = new Date().getTime() - startTime;
4145
let remainingTime = 24 * 60 * 60 * 1000 - elapsedTime;
4246

4347
if (remainingTime > 0 && couponName) {
44-
displayPromotionDetails(remainingTime);
48+
let hours = Math.floor((remainingTime % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
49+
let minutes = Math.floor((remainingTime % (1000 * 60 * 60)) / (1000 * 60));
50+
let seconds = Math.floor((remainingTime % (1000 * 60)) / 1000);
51+
52+
document.querySelectorAll('[data-co-offer="promo-timer"]').forEach(function(timerDisplay) {
53+
timerDisplay.textContent = `${hours}h ${minutes}m ${seconds}s`;
54+
});
4555
} else {
4656
clearInterval(timerInterval);
4757
localStorage.setItem('timer_finished', 'true');
@@ -51,21 +61,9 @@ function updateTimer(couponName, startTime, timerInterval) {
5161
}
5262
}
5363

54-
function displayPromotionDetails(remainingTime) {
55-
let hours = Math.floor((remainingTime % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
56-
let minutes = Math.floor((remainingTime % (1000 * 60 * 60)) / (1000 * 60));
57-
let seconds = Math.floor((remainingTime % (1000 * 60)) / 1000);
58-
59-
document.querySelectorAll('[data-co-offer="promo-timer"]').forEach(function(timerDisplay) {
60-
timerDisplay.textContent = `${hours}h ${minutes}m ${seconds}s`;
61-
});
62-
document.querySelectorAll('[data-co-offer="promo-code-wrapper"]').forEach(function(promoPopinElement) {
63-
promoPopinElement.style.display = 'block';
64-
});
65-
}
66-
6764
function updatePromoCode() {
6865
let memberData = localStorage.getItem('_ms-mem');
66+
6967
if (memberData) {
7068
let memberObj = JSON.parse(memberData);
7169
if (memberObj && memberObj.metaData && memberObj.metaData.coupon_name) {
@@ -79,9 +77,14 @@ function updateCouponDisplay(couponName) {
7977
promoElements.forEach(function(element) {
8078
element.textContent = couponName;
8179
});
80+
81+
if (!localStorage.getItem('timer_finished')) {
82+
startCouponTimer(couponName);
83+
}
8284
}
8385

8486

87+
8588
/*--- DÉBUT : DÉVEROUILLAGE FORMATION EN FONCTION DU PLAN DANS LA NAVIGATION ----*/
8689
async function updateTabLinksAndHideElementsForSpecificPlans() {
8790
const response = await window.$memberstackDom.getCurrentMember();

0 commit comments

Comments
 (0)