如何重新启动jquery中的setInterval,以便当用户点击计数器时重新启动

How to restart the setInterval in jquery so that when the user Click the counter restarts?

本文关键字:重新启动 用户 计数器 便当 jquery 中的 setInterval      更新时间:2023-09-26

如何重新启动jquery中的setInterval,以便当用户点击计数器时重新启动?

所以我创建了一个选项卡部分,以便它将循环到下一个选项卡后12秒。然而,我想重新启动时间,如果用户单击所需的选项卡,以便用户获得一个新的12秒。

我是jquery的新手,所以请详细解释。多谢。

$("li.tab").click(function () {

    $("li.tab").removeClass("select");
       $(".featured_content").hide(); //Hide all tab content
    var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
    $(activeTab).fadeIn(1600); //Fade in the active ID content
    $(this).addClass("select");


});
var refreshIntervalId = setInterval(function () {
    var lastChild = $('li.select').is(':last-child');
    if (lastChild) {
        $('li.tab').removeClass('select ')
        $("li.tab:first").addClass('select ');
        return false;
    } else {
        $("li.select").removeClass('select').next().addClass('select');
        return false;
    }
}, 12000);
//Stop
clearInterval(refreshIntervalId);
//Start again
setInterval(function () {
  ...
}, 12000)

使用clearInterval:

$("li.tab").click(function () {
    // ...
    refreshIntervalId = clearInterval(refreshIntervalId);
    // setInterval again to re-start the 'timer'
    // ...
});