简体   繁体   English

使用JQuery Cookie每周只显示一次Bootstrap模式

[英]Displaying a Bootstrap Modal just once a Week with JQuery Cookie

I am trying to get my modal only displayed once a week. 我想让我的模态每周只显示一次。 This should be guilty for all pages the user visits on my homepage. 对于用户在我的主页上访问的所有页面,这应该是有罪的。 The modal it self works just fine. 它自己的模态很好。 But it appears each time I reload the page or go to another page. 但每次我重新加载页面或转到另一页时都会出现。

Thanks a lot in advance! 非常感谢提前!

For the cookie I use this: 对于cookie我使用这个:

https://github.com/js-cookie/js-cookie https://github.com/js-cookie/js-cookie

Here is my code: 这是我的代码:

 // MODAL APPEAR AFTER 2 SECONDS setTimeout(function() { $('#newsletter-modal').modal(); }, 2000); // MODAL COOKIE FOR 7 DAYS $(function() { if($.cookie('alreadyShow') === null) { $.cookie('alreadyShow', true, {expires: 7}); $('#newsletter-modal').modal({ display: 'block' }); } }); 
 <script type="text/javascript" src="/js/js.cookie.js"></script> <div class="modal fade" id="newsletter-modal"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button> <h1 class="modal-title">Modal Headline</h1> </div> <div class="modal-body"> <p>Modal content goes here</p> </div> <div class="modal-footer"> <button type="button" class="btn btn-primary newsletter-modal-close" data-dismiss="modal">Yes</button> <button type="button" class="btn btn-default newsletter-modal-close" data-dismiss="modal">No</button> </div> </div><!-- /.modal-content --> </div><!-- /.modal-dialog --> </div><!-- /.modal --> 

Currently, the Timeout for your modal gets set every time the page is loaded, regardless of the cookie settings. 目前,无论cookie设置如何,每次加载页面时都会设置模态的超时。 Try moving that block into the function where you set the cookie. 尝试将该块移动到您设置cookie的功能中。 That way, it only gets called when a cookie doesn't already exist. 这样,它只在cookie尚不存在时被调用。

// MODAL COOKIE FOR 7 DAYS
$(function() {
    if($.cookie('alreadyShow') === null) {
        // MODAL APPEAR AFTER 2 SECONDS
        setTimeout(function() {
            $('#newsletter-modal').modal();
        }, 2000);

        $.cookie('alreadyShow', true, {expires: 7});

        $('#newsletter-modal').modal({
            display: 'block'
        });
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM