Klick February 2016

Doubled time counting

After page load function "intro" is launch. This function displays two buttons, after click on one of them another function "startGame" starts with parameter "innerHTML" from click button. Then the right panel appears and "START" button counts time from 10. Countdown stops when user click on map or time reaches 0. After "START" user can click on map (to add a marker) only ones, and click "START" again to add another marker.

When users click on map 4 times "game" finishes and two buttons appear again.

And that is when a problem starts. When function "startGame" starts again and user clicks "START" button, countdown doubles (you can see it in console). If user clicks on map one countdown stops but second still counts to zero.

Can anyone tell me why time is doubled? Link to live version: http://najlepszekomisy.co.pl/

Thank you.

var elem =
{
    map: document.getElementById("mapa"),
    panel: document.getElementById("right_panel"),
    games: document.getElementById("games"),
    draw: document.getElementById("draw"),
    points: document.getElementById("pointsGet"),
    timer: document.getElementById("timer")
};
(function intro()
{
    elem.games.addEventListener("click",function(e){
        if(e.target.tagName==="H4")
        {
            TweenMax.to(elem.games,1,{ease: Back.easeOut,top: -50,onComplete:function(){
            startGame(e.target.innerHTML);}})
        }
    },false)
})();

function startGame(hit)
{

  var gameElement =
    {
        mapa:0,
        clickListener:0,
        number:0,
        usingSet:4,
        timeNum:10
    };

    (function loadMap()
    {
        var mapOptions =
        {
            zoom: 7,
            disableDefaultUI: true,
            zoomControl: true,
            center: new google.maps.LatLng(51.95442214470796, 19.14093017578125)
        };
        gameElement.mapa = new go        

Answers


Scott Marcus February 2016

You need to make sure that the calcTime function which includes this code:

 timer.startTime = setTimeout(timer.calcTime, 1000);

cannot be invoked while it is already running, because if it does, then another call to calcTime will be queued up causing the clock to tick twice as fast.


Maxim Gritsenko February 2016

Every time your H4 button is clicked it calls startGame function. Every time startGame function is called it adds one more event listener to the Start button, so start handler is called once when you play first time, twice the second time, thrice next time etc.

To avoid this you can:

Post Status

Asked in February 2016
Viewed 2,933 times
Voted 11
Answered 2 times

Search




Leave an answer