How can I stop seconds from 60 to 0

Hello new to javascript and html, I just want to know how can I stop the third textbox (txtS) from 60 to 0. I am trying to create a clock, so the seconds need to stop at 60 and the minutes should start to increase. **

I'm only asking how to stop seconds at 60

<html >
<head>
    <title>clock</title>
    <script language="javascript">
        function fn_sample() {
            document.frm.txtS.value = parseInt(document.frm.txtS.value) + 1;
            var t1 = document.frm.txtS.value;
            if(t1>=60){

                t1 = 0;

            }
            window.setTimeout("fn_sample()", 1000);

        }


    </script>
</head>
<body>
    <form name="frm">
        <input type="text" name="txtH" value="0" size="2"/>
        <input type="text" name="txtM" value="0" size="2"/>
        <input type="text" name="txtS" value="0" size="2"/>
        <br /><br />
        <input type="button" value="Start" onclick="fn_sample();" />
    </form>
</body>

      

+3


source to share


1 answer


Just add else

after the closure to make it setTimeout

dependent on the previous test.

(For demonstration, I reduced the wait time to 100

6 seconds instead of 60 ;-). Please note that to ensure a full cycle of at least 1 second, you cannot sleep 1 second between each operation! They prefer to use: window.setTimeout("fn_sample()",1000-(new Date()%1000));

. This will work until fn_sample()

the execution time reaches 1 second.



        function fn_sample() {
            document.frm.txtS.value = parseInt(document.frm.txtS.value) + 1;
            var t1 = document.frm.txtS.value;
            if(t1>=60){

                document.frm.txtS.value = 0;

            } else
            window.setTimeout("fn_sample()",
                              100-(new Date()%100));

        }
      

    <form name="frm">
        <input type="text" name="txtH" value="0" size="2"/>
        <input type="text" name="txtM" value="0" size="2"/>
        <input type="text" name="txtS" value="0" size="2"/>
        <br /><br />
        <input type="button" value="Start" onclick="fn_sample();" />
    </form>
      

Run codeHide result


+1


source







All Articles