[英]Start countdown in current active element
I have bootstrap multi-step form.我有引导程序多步骤形式。 There's <span class="timer"></span>
at each step.每一步都有<span class="timer"></span>
。 My problem is that timer works only at first step.我的问题是计时器只在第一步工作。 I don't know how to start countdown on another step (fieldset tag).我不知道如何在另一个步骤(fieldset 标签)上开始倒计时。 Timer code:定时器代码:
var counter = seconds;
var interval = setInterval(() => {
document.getElementById('timer').innerHTML = "("+counter + " seconds left)" ;
counter--;
if(counter < 0 ){
clearInterval(interval);
};
}, 1000);
};
I also tried this:我也试过这个:
$("#nextStep").click(function() {
startCountdown(60);
});
document.getElementById('timer')
always returns the first item found in the DOM! document.getElementById('timer')
总是返回在 DOM 中找到的第一项! you should use different id's ( #timer-1
, #timer-2
, etc) and select them conditionally as you progress through each step.您应该使用不同的 ID( #timer-1
、 #timer-2
等)并在您完成每个步骤时有条件地选择它们。
you can augment your startCountdown
method like this:你可以像这样增加你的startCountdown
方法:
function startCountDown (selectorId, seconds) {
var counter = seconds;
var interval = setInterval(() => {
document.getElementById(selectorId).innerHTML = "("+counter + " seconds left)" ;
counter--;
if(counter < 0 ){
clearInterval(interval);
};
}, 1000);
};
let idCounter = 0
$("#nextStep").click(function() {
// generate new counter id based on your progression
// this assumes your ids are timer-0, timer-1, etc
startCountdown(60, 'timer-' + idCounter);
idCounter++
});
Even better approach would be to use classes and getElementsByClassName()
and then simply iterate over the array:更好的方法是使用类和getElementsByClassName()
然后简单地遍历数组:
function startCountDown (selectorId, timerCounter, seconds) {
var counter = seconds;
var interval = setInterval(() => {
// getElementsByClassName returns a DOMNodeList of matched items that you can use indexes to access
document.getElementByClassName(selectorId)[timerCounter].innerHTML = "("+counter + " seconds left)" ;
counter--;
if(counter < 0 ){
clearInterval(interval);
};
}, 1000);
};
let timerCounter = 0
$("#nextStep").click(function() {
startCountdown(60, 'timer', timerCounter);
timerCounter++
});
You need to change <span class="timer"></span>
to <span id="timer"></span>
.您需要将<span class="timer"></span>
更改为<span id="timer"></span>
。 And with };
并与};
at the end in gives an error for me, so I used最后给我一个错误,所以我用
var counter = seconds;
var interval = setInterval(() => {
document.getElementById('timer').innerHTML = "("+counter + " seconds left)" ;
counter--;
if(counter < 0 ){
clearInterval(interval);
};
}, 1000);
instead of代替
var counter = seconds;
var interval = setInterval(() => {
document.getElementById('timer').innerHTML = "("+counter + " seconds left)" ;
counter--;
if(counter < 0 ){
clearInterval(interval);
};
}, 1000);
};
You can do你可以做
var counter = seconds;
function startInterval() {
var interval = setInterval(() => {
document.getElementById('timer').innerHTML = "("+counter + " seconds left)" ;
counter--;
if(counter < 0 ){
clearInterval(interval);
};
}, 1000);
}
and use startInterval()
to start the timer.并使用startInterval()
启动计时器。 If you need to do something when the timer seconds are over, you can do如果你需要在计时器秒数结束时做某事,你可以做
var counter = seconds;
function startInterval() {
var interval = setInterval(() => {
document.getElementById('timer').innerHTML = "("+counter + " seconds left)" ;
counter--;
if(counter < 0 ){
clearInterval(interval);
var timerOver = true;
};
}, 1000);
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.