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
 |  Show 5 more comments

2 Answers 2

Reset to default 32

You 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