Я использую JQuery для настройки счетчика с 60, и он отлично работает. за исключением случаев, когда он отсчитывает первый раунд и достигает нуля, когда я его сбрасываю, он сначала показывает ноль, а затем снова начинается с 60. Я хочу, чтобы он сразу запускался с 60 после сброса. Есть идеи? вот мой код (это пример кода, который дает тот же результат, что и мой исходный код)
$("#Resend-confirm-phone-number-code").hide();
var count = 60,
timer = setInterval(function () {
$("#counter").html(--count);
if (count == 0) {
clearInterval(timer);
$("#resend").fadeOut("fast", function () { });
$("#Resend-confirm-phone-number-code").fadeIn("fast", function () { });
}
},
1000);
$("#resend").show();
function aliAsghar() {
$("#Resend-confirm-phone-number-code").hide();
var count = 60,
timer = setInterval(function () {
$("#counter").html(--count);
if (count == 0) {
clearInterval(timer);
$("#resend").fadeOut("fast", function () { });
$("#Resend-confirm-phone-number-code").fadeIn("fast", function () { });
}
},
1000);
$("#resend").show();
}
<script src = "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class = "newRegister-addCode" id = "confirmPhoneNumberForm-modal" tabindex = "-1" role = "dialog" aria-labelledby = "myModalLabel"
aria-hidden = "true">
<div class = "newRegister-helpText">
<p>
enter the code
</p>
</div>
<form class = "newRegister-formBox" method = "Post" id = "confirmPhoneNumberForm" autocomplete = "off">
<div class = "formElement newRegister-input">
<i class = "fa fa-key"></i>
<input autocomplete = "off" type = "text" name = "code" id = "code" class = "form-control persianDigitInput"
placeholder = "enter the code">
<div class = "newRegister-alarmText">
<p id = "resend"> seconds left<span id = "counter" >60</span></p>
<button class = "animateBtn" type = "button" id = "Resend-confirm-phone-number-code" style = "Display:none; " onclick = "aliAsghar()">
<i class = "fa fa-refresh"></i>
send again
</button>
</div>
</div>
<div class = "newRegister-button">
<button type = "button" id = "send-confirm-phone-number-code" class = "animateBtn" >
<i class = "fa fa-check"></i>
submit
</button>
</div>
</form>
</div>
@SmitRaval всегда предпочтительнее иметь работающий фрагмент здесь, а не за пределами сайта.
Я добавил html-код
Вы можете решить эту проблему, немедленно обновив HTML-код элемента значением count
при первом вызове функции и до того, как произойдет первая итерация интервала.
Затем вы можете высушить логику, просто вызвав aliAsghar()
при загрузке страницы, вместо того, чтобы дважды копировать одну и ту же логику на страницу.
Обратите внимание на использование ненавязчивого обработчика событий над атрибутами событий on*
в HTML, которого следует по возможности избегать, а также на удаление пустых функций обратного вызова в вызовах fadeX()
, которые не являются обязательными.
function aliAsghar() {
$("#Resend-confirm-phone-number-code").hide();
var count = 5; // set to 5 just to speed up testing
$("#counter").html(count);
var timer = setInterval(function() {
$("#counter").html(--count);
if (count == 0) {
clearInterval(timer);
$("#resend").fadeOut("fast");
$("#Resend-confirm-phone-number-code").fadeIn("fast");
}
}, 1000);
$("#resend").show();
}
aliAsghar(); // onload
$('#Resend-confirm-phone-number-code').click(function() {
aliAsghar();
});
<script src = "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class = "newRegister-addCode" id = "confirmPhoneNumberForm-modal" tabindex = "-1" role = "dialog" aria-labelledby = "myModalLabel" aria-hidden = "true">
<div class = "newRegister-helpText">
<p>
enter the code
</p>
</div>
<form class = "newRegister-formBox" method = "Post" id = "confirmPhoneNumberForm" autocomplete = "off">
<div class = "formElement newRegister-input">
<i class = "fa fa-key"></i>
<input autocomplete = "off" type = "text" name = "code" id = "code" class = "form-control persianDigitInput" placeholder = "enter the code">
<div class = "newRegister-alarmText">
<p id = "resend"> seconds left<span id = "counter">60</span></p>
<button class = "animateBtn" type = "button" id = "Resend-confirm-phone-number-code" style = "Display:none;">
<i class = "fa fa-refresh"></i>
send again
</button>
</div>
</div>
<div class = "newRegister-button">
<button type = "button" id = "send-confirm-phone-number-code" class = "animateBtn">
<i class = "fa fa-check"></i>
submit
</button>
</div>
</form>
</div>
Просто установите: $('#counter').text( "60" );
$("#Resend-confirm-phone-number-code").hide();
$('#counter').text( "60" );
var count = 60,
timer = setInterval(function () {
$("#counter").html(--count);
if (count == 0) {
clearInterval(timer);
$("#resend").fadeOut("fast", function () { });
$("#Resend-confirm-phone-number-code").fadeIn("fast", function () { });
}
},
1000);
$("#resend").show();
function aliAsghar() {
$("#Resend-confirm-phone-number-code").hide();
$('#counter').text( "60" );
var count = 60,
timer = setInterval(function () {
$("#counter").html(--count);
if (count == 0) {
clearInterval(timer);
$("#resend").fadeOut("fast", function () { });
$("#Resend-confirm-phone-number-code").fadeIn("fast", function () { });
}
},
1000);
$("#resend").show();
}
<script src = "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class = "newRegister-addCode" id = "confirmPhoneNumberForm-modal" tabindex = "-1" role = "dialog" aria-labelledby = "myModalLabel"
aria-hidden = "true">
<div class = "newRegister-helpText">
<p>
enter the code
</p>
</div>
<form class = "newRegister-formBox" method = "Post" id = "confirmPhoneNumberForm" autocomplete = "off">
<div class = "formElement newRegister-input">
<i class = "fa fa-key"></i>
<input autocomplete = "off" type = "text" name = "code" id = "code" class = "form-control persianDigitInput"
placeholder = "enter the code">
<div class = "newRegister-alarmText">
<p id = "resend"> seconds left<span id = "counter" ></span></p>
<button class = "animateBtn" type = "button" id = "Resend-confirm-phone-number-code" style = "Display:none; " onclick = "aliAsghar()">
<i class = "fa fa-refresh"></i>
send again
</button>
</div>
</div>
<div class = "newRegister-button">
<button type = "button" id = "send-confirm-phone-number-code" class = "animateBtn" >
<i class = "fa fa-check"></i>
submit
</button>
</div>
</form>
</div>
Чтобы упростить код, вы можете просто вызвать функцию:
/*$("#Resend-confirm-phone-number-code").hide();
$('#counter').text( "60" );
var count = 60,
timer = setInterval(function () {
$("#counter").html(--count);
if (count == 0) {
clearInterval(timer);
$("#resend").fadeOut("fast", function () { });
$("#Resend-confirm-phone-number-code").fadeIn("fast", function () { });
}
},
1000);
$("#resend").show();*/
aliAsghar();
function aliAsghar() {
$("#Resend-confirm-phone-number-code").hide();
$('#counter').text( "60" );
var count = 60,
timer = setInterval(function () {
$("#counter").html(--count);
if (count == 0) {
clearInterval(timer);
$("#resend").fadeOut("fast", function () { });
$("#Resend-confirm-phone-number-code").fadeIn("fast", function () { });
}
},
1000);
$("#resend").show();
}
<script src = "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class = "newRegister-addCode" id = "confirmPhoneNumberForm-modal" tabindex = "-1" role = "dialog" aria-labelledby = "myModalLabel"
aria-hidden = "true">
<div class = "newRegister-helpText">
<p>
enter the code
</p>
</div>
<form class = "newRegister-formBox" method = "Post" id = "confirmPhoneNumberForm" autocomplete = "off">
<div class = "formElement newRegister-input">
<i class = "fa fa-key"></i>
<input autocomplete = "off" type = "text" name = "code" id = "code" class = "form-control persianDigitInput"
placeholder = "enter the code">
<div class = "newRegister-alarmText">
<p id = "resend"> seconds left<span id = "counter" ></span></p>
<button class = "animateBtn" type = "button" id = "Resend-confirm-phone-number-code" style = "Display:none; " onclick = "aliAsghar()">
<i class = "fa fa-refresh"></i>
send again
</button>
</div>
</div>
<div class = "newRegister-button">
<button type = "button" id = "send-confirm-phone-number-code" class = "animateBtn" >
<i class = "fa fa-check"></i>
submit
</button>
</div>
</form>
</div>
Не могли бы вы создать скрипку для того же?