简体   繁体   中英

ASP.NET MVC5, creating a timer for quiz using javascript

Here is the code I use, I can't figure out why at 0 seconds, the form won't submit to the thank you page. I am trying to call the submit button from the @using (htmlbeginform()) statement. Please help.

<form name="counter">
    <input type="text" size="8"
           name="d2">
</form>


<script type="text/javascript">
    var minutes = 1
    var seconds = 00

    document.counter.d2.value = '30:00'

    function display()
    {
        if (seconds <= 0)
        {
            minutes -= 1
            seconds += 59
        }
        if (minutes <= -1)
        **{
            $(document).ready(function () {
                setTimeout(function () { nextQuestion() }, 5000);
                $("#questionform").submit(function () {
                    alert("Submit after 5 second.");
                });
            });
            function nextQuestion() {
                $("#questionform").trigger("submit");
            }

        }**  **PART of code in between ** is where I am having trouble, cant figure out what is wrong**

        else
            seconds -= 1
        document.counter.d2.value = minutes + ":" + seconds
        setTimeout("display()", 1000)
    }
    display()
</script>





@using (Html.BeginForm("ThankYou", "Questions", FormMethod.Post, new { @id = "questionform" }))
{
    @Html.EditorFor(x => x.Questions)

    <input type="submit" id="submit" class="btn btn-default" value="Submit" />
}

TRYING TO USE THIS BUTTON DOWN HERE to SUBMITAT ZERO SECONDS

the $(document).ready() event triggers only when your document is fully loaded, since you attach this event after the document is loaded, this will never trigger.

you have to do it in this way:

function nextQuestion() {
            $("#questionform").trigger("submit");
        } //it's better declare your functions outside an if statment

if (minutes <= -1) {           
            setTimeout(function () { nextQuestion() }, 5000);
            $("#questionform").submit(function () {
                alert("Submit after 5 second.");
            }); 
            /*if you submit, 
            the page changes and this alert will never appear*/
        });            
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM