Javascript : Auto submit form after timer/countdown in Javascript / html

Let's create a test.php file please check it. form submit on timer/countdown you can pass time through in javascript CountDown(5,div); function, example 10 second pass CountDown(10,div);




 <html>
            <body>
              <form action="" method="post" name="MCQuestion" id="MCQuestion">
                Name:<input type="test" name="name" value="ABC">
              <div><span id="display" style="color:#FF0000;font-size:15px"></span></div>
            <div><span id="submitted" style="color:#FF0000;font-size:15px"></span></div>
            </form>
            <script>
            var div = document.getElementById('display');
            var submitted = document.getElementById('submitted');

              function CountDown(duration, display) {

                        var timer = duration, minutes, seconds;

                      var interVal=  setInterval(function () {
                            minutes = parseInt(timer / 60, 10);
                            seconds = parseInt(timer % 60, 10);

                            minutes = minutes < 10 ? "0" + minutes : minutes;
                            seconds = seconds < 10 ? "0" + seconds : seconds;
                    display.innerHTML ="<b>" + minutes + "m : " + seconds + "s" + "</b>";
                            if (timer > 0) {
                               --timer;
                            }else{
                       clearInterval(interVal)
                                SubmitFunction();
                             }

                       },1000);

                }

              function SubmitFunction(){
                submitted.innerHTML="Time is up!";
                document.getElementById('MCQuestion').submit();

               }
               CountDown(5,div);
            </script>
            </body>
            </html>


5 comments:

  1. Thank You this is help me a lot https://phptechidea.blogspot.com/

    ReplyDelete
  2. How to stop countdown timer in javascript?

    ReplyDelete
  3. How to prevent form submit in javascript?

    ReplyDelete
  4. timer is working perfectly fine.but it is not submitting form for me pls why?

    ReplyDelete