16

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 help ?

Karthick Terror
  • 801
  • 2
  • 8
  • 11

5 Answers5

29

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>

......

redShadow
  • 6,687
  • 2
  • 31
  • 34
  • 1
    It's recommended where possible to send function references to `setTimeout` and not strings, since they're `eval`ed which is slow and unsafe. In this case, it's also shorter to just write `setTimeout(gameLost, 60000)` – davin Aug 01 '11 at 00:05
  • @davin: does this work? Usually, I use jQuery so I don't remember usage of these "standard" functions.. I quickly checked documentation that doesn't mention that. – redShadow Aug 01 '11 at 00:07
  • If it didn't work, I wouldn't suggest it :) See https://developer.mozilla.org/En/Window.setTimeout the syntax clearly says `func` and describes it as a function. Plus, they clearly state that the string version is not recommended. But if you want to be 100% sure, look at their examples; it is exactly the same structure as yours and they do what I suggested. – davin Aug 01 '11 at 00:11
  • If anyone interested, that's what I come out with after playing a bit with that code.. :) http://stuff.hackzine.org/misc-code/js-timeout.html – redShadow Aug 01 '11 at 00:35
8
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();
Flambino
  • 18,507
  • 2
  • 39
  • 58
  • +1 for being first (you beat me by about five seconds, but given my answer was the same as yours I didn't post it). – nnnnnn Aug 01 '11 at 00:01
  • First argument to `setTimeout()` is a JavaScript statement, not a callback.. so it should be `'timedOut()'` instead – redShadow Aug 01 '11 at 00:02
  • @Flambino . Thanks . I am also need to display the timer . – Karthick Terror Aug 01 '11 at 00:02
  • 1
    @redShadow: No, opposite. Using a string of javascript code is the outdated method of doing it. Passing a callback is the preferred way of doing it. – Flambino Aug 01 '11 at 00:10
  • @Flambino, sorry, you are right. I quickly checked documentation (on stupid w3schools...) that did not mention that.. – redShadow Aug 01 '11 at 00:16
  • @redShadow: Why are you looking at W3S when [MDN](https://developer.mozilla.org/en/JavaScript) is available? – mu is too short Aug 01 '11 at 00:17
  • @KarthickTerror: Well, you didn't say anything about that in your question, but ok... here's a jsfiddle that pretty much does that http://jsfiddle.net/HRrYG/ – Flambino Aug 01 '11 at 00:17
  • @muistooshort: heh, wasn't it just yesterday that you warned against W3S over on this question: http://stackoverflow.com/questions/6886031/force-loading-an-image-to-stop-flickering/6886048 ? :) – Flambino Aug 01 '11 at 00:21
  • @Flambino: I think I did a couple other times today too, just fighting the good fight for decent documentation :) – mu is too short Aug 01 '11 at 00:28
  • @muistooshort: I couldn't agree more, so by all means keep it up :) – Flambino Aug 01 '11 at 00:36
1

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.

qw3n
  • 6,236
  • 6
  • 33
  • 62
0

// 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

Community
  • 1
  • 1
Narendra Maurya
  • 391
  • 2
  • 7
0

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>
      
      
      
      
Masud Rana
  • 590
  • 1
  • 8
  • 17