We have answer of your question!

100% solved queries, no empty question

Question: Start time once game is complete


0

Advertisement


JSFiddle: JS FIDDLE

The HTML:

<div id="content">
    <input type=button value="Start Game" id=sGame class=sGame />
    Timer: <input type=text class=timer id=timer value="" READONLY />

    <br><br>
    <div id="cardPile"> </div>
    <div id="cardSlots"> </div>
    <div id="successMessage">
        <h2>You did it in <span class="time"></span> seconds!</h2>
        <button onclick="init()">Play Again</button>
    </div>
</div>

The issue I am having is once the PLAY AGAIN button is clicked the timer is not restarting from 0. I tried the following code

$('#sGame').click();

inside the

init();

function, but the timer starts off even before the Start Game button is clicked.

Question author Si8 | Source

Answer


1


Advertisement


if (timer !== null) clearInterval(timer), value = 0;

If the timer is NOT null (it's running) -> clear the interval, set the value variable to 0, then restart the interval.

$('#sGame').click(function () {
    if (timer !== null) clearInterval(timer), value = 0;
    timer = setInterval(function () {
        value = value + 1;
        $("#timer").val(value);
    }, interval);
    $('#cardPile').show('slow');
    $('#cardSlots').show('slow');
});
Answer author Pmandell

Advertisement


Tickanswer.com is providing the only single recommended solution of the question Start time once game is complete under the categories i.e jquery , html , . Our team of experts filter the best solution for you.

Related Search Queries:

You may also add your answer!