Have a reset button and start a countdown timer

I am trying to start a countdown timer when a button is pressed. I searched and found several different items but nothing seems to work.

I have different buttons all over the domain that will set different times (IE: 120 seconds, 60 seconds, etc.). The display is in one place on every page ( div

top right corner of the loaded page), so I wanted to use external jsscripts/timer.js

I can use <div id="timer"></div>

to place a timer.

My questions are: How to call a function to start a timer. What good does the script suggest for the said timer, when the amount of time might change and it should wrap pages (I used $_SESSION['x']

to pass information between .php

<div class="headertopgab">
    <div style="float: left; margin-left: 20px">
        Galactic Credits: 
        <?php echo "&Ccedil;".$_SESSION['galacticCredits']; ?>
    </div>
    <div style="margin-left: 50px; float: left">
        <?php echo "".$_SESSION['currentLocation']; ?>
    </div>
    <div style="float:right">
        <div id="time">10</div>  <!-- this is where I want the countdown-->
    </div>
</div>

      

+3


source to share


1 answer


you can use this

var countdown;
var counter = function(){
    var time = parseInt($('#time').text());
    if (time !== 0){
        $('#time').text(time - 1);
    }else{
        clearInterval(countdown);
    }
}
countdown = setInterval(counter , 1000);

      



DEMO

+5


source







All Articles