我在clearInterval时遇到问题。请检查this link并多次点击HERE
。然后你可以看到我面临的问题。目标是当用户点击HERE
然后setInterval
应该被清除并再次从1开始。您也可以查看下面的代码
<script class="cssdeck" src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<div class="otpIcon">
<span class="resend">
Click <a href="javascript:void(0)" id="resendForgotPasswordOtpLink" data-url="/user/resend-forgot-password-otp">HERE</a> to resend OTP
</span>
<span class="setTimer" style="display: inline;">
You will get OTP within <span class="timer">6 seconds</span>
</span>
</div>
function otpTimer(container, count) {
container.find('timer').html(count + ' seconds');
setInterval(function() {
if (count < 1) {
container.find('.resend').show();
container.find('.setTimer').hide();
} else {
count--;
container.find('.timer').html(count + ' seconds');
}
}, 1000);
}
function stopOtpTimer(clearme) {
window.clearInterval(clearme);
}
$('#resendForgotPasswordOtpLink').click(function(e) {
alert('sdf')
var count = 30;
var container = $('.otpIcon');
var timer;
container.find('.resend').show();
container.find('.setTimer').show();
stopOtpTimer(timer);
timer = otpTimer(container, count);
})