开发者

How to set one minute counter in javascript?

In my project ,I have list of questions, for every question have three option answers.

After see the question if i want answer that question means click "show answer" button . when I click button ,counter starts for one minute after one minute error will show .

can any one he开发者_运维知识库lp ?


You could use something like this:

function gameLost() {
  alert("You lose!");
}
setTimeout(gameLost, 60000);

UPDATE: pass function reference to setTimeout() instead of code string (did I really write it that way? O_o)


EDIT

To display the timer too (improved version, thanks to davin too):

<button onclick="onTimer()">Clickme</button>
<div id="mycounter"></div>
<script>
i = 60;
function onTimer() {
  document.getElementById('mycounter').innerHTML = i;
  i--;
  if (i < 0) {
    alert('You lose!');
  }
  else {
    setTimeout(onTimer, 1000);
  }
}
</script>

......


function timedOut() {
    alert("Some error message");
}

// set a timer
setTimeout( timedOut , 60000 );

That basically sets a timer that will execute the given function after 60.000 miliseconds = 60 seconds = 1 minute

Edit: here's a quick, imperfect fiddle that also shows the countdown http://jsfiddle.net/HRrYG

function countdown() {
    var seconds = 60;
    function tick() {
        var counter = document.getElementById("counter");
        seconds--;
        counter.innerHTML = "0:" + (seconds < 10 ? "0" : "") + String(seconds);
        if( seconds > 0 ) {
            setTimeout(tick, 1000);
        } else {
            alert("Game over");
        }
    }
    tick();
}

// start the countdown
countdown();


You will want to use the setTimout function check out this article. https://developer.mozilla.org/En/Window.setTimeout Remember the timer is in milliseconds so for one minute is 60,000.


// this is the simplest way to one mint counter .this is also use in angular and oops

var i=60;
function coundown(){
   setInterval(() => {
  if (this.i == 0) {
    return;
  }
  console.log(this.i--);

}, 1000);
}

// this function you can call when otp is comming or form submit and waiting for otp countdown

angular #javascript #typescript


you can try to use this

or visit for more details Demo

Demo2

function countdown() {
        var seconds = 59;
        function tick() {
          var counter = document.getElementById("counter");
          seconds--;
          counter.innerHTML =
            "0:" + (seconds < 10 ? "0" : "") + String(seconds);
          if (seconds > 0) {
            setTimeout(tick, 1000);
          } else {
            document.getElementById("verifiBtn").innerHTML = `
                <div class="Btn" id="ResendBtn">
                    <button type="submit">Resend</button>
                </div>
            `;
            document.getElementById("counter").innerHTML = "";
          }
        }
        tick();
      }
      countdown();
<div class="btnGroup">
        <span class="Btn" id="verifiBtn">
          <button type="submit">Verify</button>
        </span>
        <span class="timer">
          <span id="counter"></span>
        </span>
      </div>
      
      
      
      

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜