-1

I am working on Question / Answer game and i need to set a countdown for each question. If time is out, call next button click.

Here is my code, demo:

https://jsfiddle.net/0ex9neh3/1/

How can I reset a timer on trigger click?

Here is a html part:

<div id="my-timer">
    Page Will Redirect with in <b id="show-time">10</b> seconds        
</div>
<input type="button" name="next" value="Next question" class="wpProQuiz_button wpProQuiz_QuestionButton next-button" style="float: right;">

Here is JS:

$(document).ready(function() {
var settimmer = 0;
$(function() {
    window.setInterval(function() {
        var timeCounter = $("b[id=show-time]").html();
        var updateTime = eval(timeCounter) - eval(1);
        $("b[id=show-time]").html(updateTime);
        if (updateTime == 0) {
            $("input[value='Next question']").trigger(
                'click');
        }
    }, 1000);
});
$("input[value='Next question']").click(function() {
    alert('timeout reset')
});

});

Foxsk8
  • 298
  • 1
  • 8
  • 21
  • This code needs a lot of refactoring. Why do you use _eval_ function instead of _parseInt_? Why do you call _eval(1)_ at all? Why do you read timer values from the DOM if you can just use a simple variable and its closure? – Ivan Mar 10 '16 at 19:26

1 Answers1

1

I assuming that you want to reset the timeout if you click on next question.

$("input[value='Next question']").click(function() {
    $("b[id=show-time]").html("10");
    alert('timeout reset')
});

If you click on next Question the timer will be resetted. Or if the timer is 0, then it will be resetted, too. Key here is $("b[id=show-time]").html("10"); to reset the timer to 10 seconds.

Kordi
  • 2,229
  • 1
  • 12
  • 11