需要每秒调用脚本,还是更好的解决方案?

时间:2017-07-26 08:22:08

标签: javascript jquery

这是我的剧本:

P: /devices/virtual/lpc_spartan/lpc_peach
N: lpc_peach
E: DEVNAME=/dev/lpc_peach
E: DEVPATH=/devices/virtual/lpc_spartan/lpc_peach
E: MAJOR=247
E: MINOR=0
E: SUBSYSTEM=lpc_spartan

我有一个弹出窗口。我有 <script> function getCookie(cname) { var name = cname + "="; var decodedCookie = decodeURIComponent(document.cookie); var ca = decodedCookie.split(';'); for (var i = 0; i < ca.length; i++) { var c = ca[i]; while (c.charAt(0) == ' ') { c = c.substring(1); } if (c.indexOf(name) == 0) { return c.substring(name.length, c.length); } } return ""; } var time_popup = <?php echo $time_popup; ?>; $(document).ready(function () { $.magnificPopup.open({ items: { src: '#recently-viewed-popup' }, type: 'inline', callbacks: { close: function closePopup() { var close = new Date(); close.setMinutes(close.getMinutes() + time_popup); document.cookie = "close_popup=" + close; } } }); }); var time_now = new Date(); var popup_close = getCookie('close_popup'); if (time_now - time_popup == popup_close) { $.magnificPopup.open({ items: { src: '#recently-viewed-popup' }, type: 'inline', callbacks: { close: function closePopup() { var close = new Date(); close.setMinutes(close.getMinutes() + time_popup); document.cookie = "close_popup=" + close; } } }); } </script> 它来自管理员,他选择分钟再次显示弹出窗口(关闭后)。当弹出窗口关闭时,我现在设置时间为自己的时间+管理员的时间+当他想要再次弹出时。

逻辑很简单:

  

现在时间 - time_popup == last_time_close_popup

但是如何每秒检查一次,逻辑是在最后一次。

2 个答案:

答案 0 :(得分:1)

您可以使用JavaScripts setTimeout()方法

这样你就不需要每秒检查一下是否已经过了5分钟。设置超时将运行您传递给其第一个参数的函数,该时间以毫秒为单位传递到第二个参数。

setTimeout(function(){
  // logic
}, 300000);

答案 1 :(得分:0)

最后,如果有人可以帮助我,那就完成了一个小错误。这是解决方案:

    </style>
    <?php if ($popup_status && !empty($products)) { ?>
        <script>
            var time_popup = <?php echo $time_popup; ?>;
            var route = '<?php echo $route; ?>';
            $(document).ready(function () {
                if ((sessionStorage.getItem('popState') != 'shown') && (route == 'common/home' || route == 'index')) {
                    sessionStorage.setItem('popState', 'shown')
                    $.magnificPopup.open({
                        items: {
                            src: '#recently-viewed-popup'
                        },
                        type: 'inline',
                        callbacks: {
                            close: function closePopup() {
                                var close = new Date();
                                $.cookie("close_popup", close);
                            }
                        }
                    });
                }
            });
            setInterval(function () {
                var time_now = new Date();
                var popup_close = $.cookie('close_popup');
                time_now.setMinutes(time_now.getMinutes() - time_popup)
                console.log(time_now);
                console.log(popup_close);
                if (time_now == popup_close) {
                    $.magnificPopup.open({
                        items: {
                            src: '#recently-viewed-popup'
                        },
                        type: 'inline',
                        callbacks: {
                            close: function closePopup() {
                                var close = new Date();
                                $.cookie("close_popup", close);
                            }
                        }
                    });
                }
            }, 1000);
        </script>

但现在当弹出窗口打开且尚未关闭且页面重新加载弹出窗口时再也无法打开。