-3

I want to start a timer at first keypress (on the ) and it should count down till 0. The catch is that once it is started it should not stop until it has reached 0 nor it should be affected by subsequent key presses.

Thank you! :)

Abhimanyu Saharan
  • 642
  • 1
  • 10
  • 26
  • Should it countdown in seconds? How should the timer be presented? – Kolban Nov 08 '14 at 04:45
  • See also: http://stackoverflow.com/questions/1191865/code-for-a-simple-javascript-countdown-timer – Kolban Nov 08 '14 at 04:47
  • I'd also suggest not posting "Can anyone help me out asap". The "asap" is likely to be read badly by potential responders. – Kolban Nov 08 '14 at 04:48
  • yes, it should be in seconds. My problem is not the timer but to start the timer at a key press and it should not be affected by the subsequent key presses – Abhimanyu Saharan Nov 08 '14 at 05:10

1 Answers1

1
<html>
<head>
  <title></title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>
<body>
    <h1 align="center"></h1>
<script>
 var time=10;
 var timer;
    $('body').keypress(function() {
        timer=setInterval(function(){
            if(time<=0)
                clearInterval(timer);
            $('h1').html(time);
            time--;
        },1000)
        $(this).unbind('keypress');
    });

</script>
</body>
</html>
kaxi1993
  • 4,535
  • 4
  • 29
  • 47
  • Thank you for your quick reply. But I don't want a button to start the timer. I want the timer to be started when a key is pressed (any one key) and it should not be affected by the subsequent key presses. – Abhimanyu Saharan Nov 08 '14 at 05:08
  • i changed code now it starts when key pressed – kaxi1993 Nov 08 '14 at 06:58
  • it does but your timer gets affected by the subsequent key presses. Is there any way to apply the concept of threads in js/jquery? With the help of threads we may be able to achieve what my problem statement states. – Abhimanyu Saharan Nov 08 '14 at 13:38
  • correct again now subsequent key presses doesn't affect – kaxi1993 Nov 08 '14 at 17:16