Show Popup Once Visit
January 30, 2025 ยท AI Conversations
if(!hasVideo && hasTime){
jQuery("#subscribe-btn").click();
clearInterval(myInterval);
}
}, 2000
show above popup once in one visiit
var myInterval = setInterval(function() {
// Check if the popup has already been shown
if (!localStorage.getItem('popupShown')) {
if (!hasVideo && hasTime) {
jQuery("#subscribe-btn").click();
clearInterval(myInterval);
// Set a flag in localStorage to indicate the popup has been shown
localStorage.setItem('popupShown', 'true');
}
} else {
// If popup already shown, clear the interval
clearInterval(myInterval);
}
}, 2000);
### Explanation:
1. **Check `localStorage`**: Before showing the popup, it checks if `popupShown` is set in `localStorage`.
2. **Show Popup**: If the popup hasn't been shown, it triggers the click on `#subscribe-btn`.
3. **Set Flag**: After showing the popup, it sets `popupShown` in `localStorage` to prevent it from showing again during the same visit.
4. **Clear Interval**: If the popup has already been shown, it clears the interval to stop further checks.
This way, the popup will only appear once per visit (as long as the user does not clear their browser's local storage).
var myInterval = setInterval(function() {
// Check if the popup has already been shown in this session
if (!sessionStorage.getItem('popupShown')) {
if (!hasVideo && hasTime) {
jQuery("#subscribe-btn").click();
clearInterval(myInterval);
// Set a flag in sessionStorage to indicate the popup has been shown
sessionStorage.setItem('popupShown', 'true');
}
} else {
// If popup already shown, clear the interval
clearInterval(myInterval);
}
}, 2000);
### Explanation:
1. **Use `sessionStorage`**: The code checks for `popupShown` in `sessionStorage`, which means the flag will be reset if the user closes the tab or browser.
2. **Show Popup**: The popup will be triggered if the flag isn't set and the conditions (`!hasVideo` and `hasTime`) are met.
3. **Set Flag**: After showing the popup, it sets `popupShown` in `sessionStorage` to prevent it from appearing again during the same session.
4. **Clear Interval**: If the popup has already been shown in the current session, it clears the interval.
This implementation ensures that the popup appears only once while the browser tab is open and will reappear if the user refreshes the page or opens a new tab.