[英]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.