How to reset setInterval function on event click on popup close

ⅰ亾dé卋堺 提交于 2019-12-13 09:52:33

问题


            <script>
                $(document).ready(function () {
                    setInterval(function () {
                        $.magnificPopup.open({
                            items: {
                                src: '#test-popup'
                            },
                            type: 'inline'
                        });
                    }, <?php echo $time_popup; ?>);
                });
            </script>

This is my script. I try to reset set interval function, when click close to popup. I use magnific popup, try with this, without result.


回答1:


You have to assign your setInterval call to a variable to be able to reset it with clearInterval.

var timer = setInterval(function() {
    // Your stuff.
    clearInterval(timer);
});

From the docs you shared:

The clearInterval() method clears a timer set with the setInterval() method.

The ID value returned by setInterval() is used as the parameter for the clearInterval() method.



来源:https://stackoverflow.com/questions/45282984/how-to-reset-setinterval-function-on-event-click-on-popup-close

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!