简体   繁体   English

单击时如何停止setinterval

[英]how to stop setinterval when click

I'm using a slideshow on my website, the plugin doesn't have an autoplay option so I'm using setinterval to achieve it. 我在我的网站上使用幻灯片播放,该插件没有自动播放选项,因此我使用setinterval来实现。

here is my Code : 这是我的代码:

var myInterval,
    startInt = function(){

        myInterval = setInterval(function(){

            $(".slidenav__item--next").click();

        }, 7000);

    };

startInt();

I have a video overlay that open when clicking on an element, what I'm trying to do is to stop my autoplay function when the overlay is opened, and start again when closing the overlay. 我有一个视频叠加层,当单击一个元素时会打开,我想做的是在打开叠加层时停止我的自动播放功能,并在关闭叠加层时重新开始。

here is what I've tried sofar but it's not working : 这是我尝试过的沙发,但不起作用:

".slides a" is the link to open my overlay, ".back" is the link to close it. “ .slides a”是打开我的叠加层的链接,“。back”是将其关闭的链接。

var myInterval,
    startInt = function(){

        myInterval = setInterval(function(){

            $(".slidenav__item--next").click();

        }, 7000);

    };

startInt();

/* RESET WHEN OVERLAY OPENED */

$(".slides a").click(function() {

    clearInterval(myInterval);

});

$(".back").click(function() {

    startInt();

});

Any help higly appreciated ! 任何帮助高加索感谢!

thanks 谢谢

I found the solution by using a flag : 我通过使用标志找到了解决方案:

var paused = false;
var autoplay = window.setInterval(function() {

    if(!paused) {

        $(".slidenav__item--next").click();

    }

}, 7000);

$('.slides a').on('click', function(e) {

    e.preventDefault();
    paused = true;

});

$(".back").click(function() {

    e.preventDefault();
    paused = false;

});

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

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