0

I am preparing an exam page. I have a time counter and it counts back from 60 seconds down to 0. There is a button NEXT, I want to skip to the next question when the time ends for the question. When you select your answer through the radio buttons and click NEXT, the page redirects to the next question, but when the time is up, I want the BUTTON be clicked automatically in order to skip to the next question.

Here is my code for counter :

function startResetTimer() {

    time = 60;
    interval = setInterval(function() {
        time--;
        document.getElementById('Label1').innerHTML = "" + time + " seconds"
    }, 1000)
}

The remaining time is shown on the label label1 and the name of my button is SubmitButton.

Aran Mulholland
  • 23,555
  • 29
  • 141
  • 228
Bahadir Alkes
  • 27
  • 1
  • 8
  • 1
    Call `click()` on the button element. – GillesC Jun 21 '15 at 22:28
  • function startResetTimer() { time = 60; interval = setInterval(function() { time--; document.getElementById('Label1').innerHTML = "" + time + " seconds" }, 1000); if (time==0) { click("SubmitButton" } – Bahadir Alkes Jun 21 '15 at 22:33
  • Something like this? – Bahadir Alkes Jun 21 '15 at 22:33
  • http://stackoverflow.com/questions/12469795/trigger-an-aspbutton-click-event-using-javascript (use the `click()` form shown) – user2864740 Jun 21 '15 at 22:51
  • 1
    You get the idea but invocation more like `document.getElementById("SubmitButton").click()` – GillesC Jun 21 '15 at 22:52
  • Edit of the post is not correct for `SubmitButton`, it's not the `name` of the button... – GillesC Jun 21 '15 at 22:54
  • possible duplicate of [Javascript: Automatically clicking a button?](http://stackoverflow.com/questions/17144459/javascript-automatically-clicking-a-button) – xrisk Jun 21 '15 at 22:55
  • Gillesc, thank you, works perfectly, here is the final code, next question comes when the timer reaches "0"..: – Bahadir Alkes Jun 21 '15 at 23:13
  • function startResetTimer2() { time = 60; interval = setInterval(function() { time--; document.getElementById('Label2').innerHTML = "" + time + " seconds"; if (time==0) { document.getElementById("SubmitButton").click() } }, 1000); } – Bahadir Alkes Jun 21 '15 at 23:13

1 Answers1

1
interval = setInterval(function() {
    time--;
    document.getElementById('Label1').innerHTML = "" + time + " seconds"
    if (time == 0) {
        // stop timer
        clearInterval(interval);
        // click
        document.getElementById('thebutton').click();            
    }
}, 1000)

Here is a JSFiddle: https://jsfiddle.net/ddan/cmtqzwa7/

DDan
  • 8,068
  • 5
  • 33
  • 52