jeudi 4 mai 2017

Stop JavaScript timer if condition is met

Building a project on Adobe Captivate and using JavaScript for timing. The penalties variable can be ignored - it changes on each slide.

I'm trying to have a timer that runs every minute and for each minute the timer runs I want to increase the weeks variable by 1. I want it to increase the weeks variable by a maximum of 2 (so max of 2 minutes) per slide. I have an if statement that checks if the currentIteration is greater than or equal to 2, if it is - I want the timer to stop - but it won't stop. It keeps on updating the weeks variable every minute. Any ideas? Code below.

penalties = penalties + 16;
weeks = weeks + penalties;

/*Timer code below*/

var currentIteration = 0;
function updateUI() {
   weeks = weeks + penalties;
    var currentTime = new Date();

    // do the diff
    var timeDiff= currentTime - startTime;

    // do the division by 1000 to strip ms
    timeDiff /= 1000;

    timeDiff = timeDiff /60;
    // do the ceiling
    Math.ceil(timeDiff);
    timeDiff = timeDiff;
    weeks = Math.floor(timeDiff)+ penalties;

    // update the UI
    window.cpAPIInterface.setVariableValue("weeks",weeks);

    currentIteration++;

if (currentIteration >= 2) {
    clearInterval(myTimer);
    window.clearInterval(myTimer);
    }

}

myTimer = setInterval(updateUI, 60000); 

Aucun commentaire:

Enregistrer un commentaire