簡體   English   中英

如何重新啟動jquery中的setInterval,以便當用戶單擊計數器時重新啟動?

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

如何重新啟動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);

使用clearInterval

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

//Start again
setInterval(function () {
  ...
}, 12000)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM