How to reset setInterval function on event click o

2019-09-20 08:04发布

            <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条回答
走好不送
2楼-- · 2019-09-20 08:13

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.

查看更多
登录 后发表回答