0

My college is organising a coding competition and for that we were tasked to build an interface which will be used on the localhost. We have achieved almost everything except one thing - auto submitting a user's answer when the timer ends. The code for the javascript is given below..

    <script>
window.onload = counter ;
function counter ()
{
    var minutes = 29;
    var seconds = 60 ;
    countDown ();
    function countDown ()
    {
        document.getElementById("mins").innerHTML = minutes ;
        document.getElementById("secs").innerHTML = seconds ;
        if ( seconds>0 )
        {
            seconds= seconds - 1 ;
            setTimeout (countDown ,1000 );
        }
        result();
        function result ()
        {
            if ( seconds == 0 )
            {
                if (minutes > 0)
                {
                    minutes = minutes -1;
                    seconds = 60;
                    countdown();        
                }
                if(minutes == 0 && seconds == 0)
                {
                    var a = parseInt(prompt("Your time is up, please contact your co-ordinators"));
                    if (a==111)
                    {
                        //document.write("Submit the form");
                    }
                    else{
                        prompt("Your time is up, please contact your co-ordinators");
                        result();
                    }
                } 
            }
        }
    }
}
</script>

How to add auto submit coding in it so that it will automatically submit user's answer.

  • You have a good counter running ..all you need to do is when the time ends, do the form submit ..cause the answer wil always bee in form elements in a form tag – Ritesh Ksheersagar Feb 01 '16 at 08:55
  • 2
    `document.getElementById("myForm").submit();` – vaso123 Feb 01 '16 at 08:55
  • 4
    Just a tip, JS is a client-side language, which means you can mess things around in the browser and manipulate values, including that of the timer in your case.You might want to involve some server side variable or so given it is a competition. – Satej S Feb 01 '16 at 08:56
  • Is there any way to prevent users from manipulating it? – subhadeep Feb 01 '16 at 09:05

3 Answers3

0

to submit a form in javascript you can get the form by id and submit it using the method .submit()

document.getElementById("myForm").submit();

also you can use the function setTimeout(); to execute any function after a specific time

setTimeout(
    function() {
        // put your code here
        document.getElementById("myForm").submit();
    },
    TIME_IN_MILLISECONDS
);

using this function is better than implementing your own method.

  • Ahh, old days in high school. We had tests in moodle with timer. Disabling javascript in browser froze the timer, and we were able to do tests without time limit >:) – Wish Feb 01 '16 at 09:04
  • haha.. i rarely use native javascript now, we have jQuery. but... good times! – Hussain Almomen Feb 01 '16 at 09:07
0

I Think you don't need to submit it and refresh the page. Maybe you need to just send data to server via ajax call. The Ajax call will send your data in specific form to the server without refreshing and updating the page.

Here's a good example.

It has the same functionality and it is more efficient.

Community
  • 1
  • 1
ebrahim.mr
  • 699
  • 5
  • 10
-1

Use this in Javascript:

document.getElementById("target").submit();

User this after you include Jquery :

$( "#target" ).submit();

Where #target is the form id.

Check this link out for more details: https://api.jquery.com/submit/