简体   繁体   中英

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

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

So i created a tabs section so that it will cycle to the next tab after 12 sec. However, I would like to restart time if the user clicks on a desired tab so that the user gets a new set of 12 sec.

I'm a novice in jquery so please explain thoroughly. Many Thanks.

$("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);

Use clearInterval :

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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