admin管理员组文章数量:1180476
I need to stop time by 02.00 to 0.00. But it stopped in -1.59 or started decrement onwards. In angular typescript i wrote above code. what i did wrong:
<html>
<body>
<input type="text" name="otp" placeholer="Enter your otp"/>
<div>Time left = <span id="timer"></span></div>
</body>
</html>
IN Angular typescript:
callMyCount(){
document.getElementById('timer').innerHTML ="02" + ":" + "00";
var myTimer=setInterval(startTimer,1000);
function startTimer() {
var presentTime = document.getElementById('timer').innerHTML;
var timeArray = presentTime.split(/[:]+/);
var m = parseInt(timeArray[0]);
var s = checkSecond((parseInt(timeArray[1]) - 1));
if(s==59){m=m-1}
if(m<0 && s==59){alert("Timeout for otp");
clearTimeout(myTimer);}
document.('timer').innerHTML = m + ":" + s;
}
function checkSecond(sec) {
if (sec < 10 && sec >= 0) {sec = "0" + sec};
if (sec < 0) {sec = "59"};
return sec;
}
}
I need to stop time by 02.00 to 0.00. But it stopped in -1.59 or started decrement onwards. In angular typescript i wrote above code. what i did wrong:
<html>
<body>
<input type="text" name="otp" placeholer="Enter your otp"/>
<div>Time left = <span id="timer"></span></div>
</body>
</html>
IN Angular typescript:
callMyCount(){
document.getElementById('timer').innerHTML ="02" + ":" + "00";
var myTimer=setInterval(startTimer,1000);
function startTimer() {
var presentTime = document.getElementById('timer').innerHTML;
var timeArray = presentTime.split(/[:]+/);
var m = parseInt(timeArray[0]);
var s = checkSecond((parseInt(timeArray[1]) - 1));
if(s==59){m=m-1}
if(m<0 && s==59){alert("Timeout for otp");
clearTimeout(myTimer);}
document.('timer').innerHTML = m + ":" + s;
}
function checkSecond(sec) {
if (sec < 10 && sec >= 0) {sec = "0" + sec};
if (sec < 0) {sec = "59"};
return sec;
}
}
Share
Improve this question
edited May 30, 2018 at 16:41
Praveen Kumar Purushothaman
167k27 gold badges213 silver badges259 bronze badges
asked May 30, 2018 at 16:38
Nivetha ElangovanNivetha Elangovan
1031 gold badge1 silver badge8 bronze badges
10
- 1 Why do you write code without indentation? – Praveen Kumar Purushothaman Commented May 30, 2018 at 16:42
- Do you need code only in Angular Typescript? Does pure JS work? – Praveen Kumar Purushothaman Commented May 30, 2018 at 16:43
- @praveenKumar i cannt understand what you are saying – Nivetha Elangovan Commented May 30, 2018 at 16:44
- js and ts are similar only. Any way i need a solution to stop decrement – Nivetha Elangovan Commented May 30, 2018 at 16:46
- Trying to replicate it. – Praveen Kumar Purushothaman Commented May 30, 2018 at 16:54
2 Answers
Reset to default 32You can do much simpler with an inner setTimeout
that performs a recursive call and counting only seconds:
let timerOn = true;
function timer(remaining) {
var m = Math.floor(remaining / 60);
var s = remaining % 60;
m = m < 10 ? '0' + m : m;
s = s < 10 ? '0' + s : s;
document.getElementById('timer').innerHTML = m + ':' + s;
remaining -= 1;
if(remaining >= 0 && timerOn) {
setTimeout(function() {
timer(remaining);
}, 1000);
return;
}
if(!timerOn) {
// Do validate stuff here
return;
}
// Do timeout stuff here
alert('Timeout for otp');
}
timer(120);
<input type="text" name="otp" placeholer="Enter your otp"/>
<div>Time left = <span id="timer"></span></div>
The timerOn
var is only here to stop countdown programmatically. Useless if loading another page.
this code is in reload page after finish timer is start again why this is problem
let timerOn = true;
function timer(remaining) {
var m = Math.floor(remaining / 60);
var s = remaining % 60;
m = m < 10 ? '0' + m : m;
s = s < 10 ? '0' + s : s;
document.getElementById('timer').innerHTML = m + ':' + s;
remaining -= 1;
if(remaining >= 0 && timerOn) {
setTimeout(function() {
timer(remaining);
}, 1000);
return;
}
if(!timerOn) {
// Do validate stuff here
return;
}
// Do timeout stuff here
alert('Timeout for otp');
}
timer(120);
本文标签: javascriptOTP verification 2 minutes count down timerStack Overflow
版权声明:本文标题:javascript - OTP verification 2 minutes count down timer - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738198466a2068278.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论