Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By Philip M

Thread: How to stop timer after 10 seconds..Here is my code ...

  1. #1
    New to the CF scene
    Join Date
    Aug 2014
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    How to stop timer after 10 seconds..Here is my code ...

    var mins = 0.10;
    var secs = mins * 60;
    function countdown() {
    setTimeout('Decrement()',1000);
    }
    function Decrement() {
    if (document.getElementById) {
    minutes = document.getElementById("minutes");
    seconds = document.getElementById("seconds");
    // if less than a minute remaining
    if (seconds < 59) {
    seconds.value = secs;
    } else {
    minutes.value = getminutes();
    seconds.value = getseconds();
    }
    secs--;
    setTimeout('Decrement()',1000);
    if (secs == 0)
    {
    alert('time up !!! You must submit your Answer ');
    }
    }
    }
    function getminutes() {
    // minutes is seconds divided by 60, rounded down
    mins = Math.floor(secs / 60);
    return mins;
    }
    function getseconds() {
    // take mins remaining (as seconds) away from total seconds remaining
    return secs-Math.round(mins *60);
    }
    </script>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,081
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    A bit abrupt! The word "please" is always encouraging. I am unclear what you are wanting. Do you want to stop the timer 10 seconds after it has started? Or when there is still 10 seconds left to run? Or what?

    Here is a script to count down from ten seconds to zero.

    Code:
    <input type="button" onclick="timer(d2,10,0)" value="Click me to start">
    <span id = "d2"></span><br>
    
    <script type="text/javascript">
    function timer(obj,s,ds){
    var SS=s;
    var DS=ds;	
    obj.innerHTML=SS+'.'+DS;
    ds--;
    if(ds<0){s--;ds=9}
    if(s>=0&&ds>=0){setTimeout(function(){timer(obj,s,ds)},100)}  // change 100 to 1000 to count down in seconds
    }
    </script>
    BTW, when posting here please help us to help you by making it easier to view, copy, modify, test and debug your scripts by following the posting guidelines and wrapping your code in CODE tags. This means use the octothorpe or # button on the toolbar.
    Last edited by Philip M; 08-12-2014 at 08:52 AM.
    infoitmanoj likes this.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    infoitmanoj (08-12-2014)

  • #3
    New to the CF scene
    Join Date
    Aug 2014
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Philip M hai dude i want stop timer after 10 seconds in my page ...timer will be stopped after 10 seconds...i have set timer mins=0.10 seconds ...but my problem was timer will be decrementing after 10seconds ...so pls help me dude ....

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,081
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by infoitmanoj View Post
    Philip M hai dude i want stop timer after 10 seconds in my page ...timer will be stopped after 10 seconds...i have set timer mins=0.10 seconds ...but my problem was timer will be decrementing after 10seconds ...so pls help me dude ....
    I don't understand your mins = 0.10 Minutes must be an integer. What is wrong with the code I gave you? And please do not call me "dude".

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    New to the CF scene
    Join Date
    Aug 2014
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    I don't understand your mins = 0.10 Minutes must be an integer. What is wrong with the code I gave you? And please do not call me "dude".
    hmm ok ....my code was working fine ....my problem was i set timer 0.10 minutes ...it means for 10 seconds .....after finish 10 seconds timer will be decremented -0,-1,-2 and so on..how can i stop timer .....

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,081
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Use the code I gave you.

    0.10 minutes is not 10 seconds!

    If you say it worked, try setting mins = 0 and secs = 10.
    Last edited by Philip M; 08-12-2014 at 12:12 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •