Simple CountDown (from 100 to 0) with JS

Hi Hans

I noticed that you already included an if/else statement in your code so I tapped into that and just added another one using the ‘stopCount’ variable you used (if variable is the right term)

So the code I used is:

     if (stopCount == 10) {
    hypeDocument.continueTimelineNamed('stop10', hypeDocument.kDirectionForward, false);

    } else if (stopCount == 50) {
    hypeDocument.continueTimelineNamed('stop50', hypeDocument.kDirectionForward, false);

    } else if (stopCount == 90) {
    hypeDocument.continueTimelineNamed('stop90', hypeDocument.kDirectionForward, false);

    } else {
      hypeDocument.continueTimelineNamed('Main Timeline', hypeDocument.kDirectionForward, false);
    }

This works like a charm. I only have one more question regarding your code. There is a part of it I cannot understand. Can you please tell me what does this snippet do?

},
intervalTime
);

Thanks a lot!

means: the codeblock above ‘intervalTime’ will be repeated every ‘intervalTime’.

the intervaltime is defined on top of the script and depends on duration and input.

Code within reset will execute when the codedown has finished. so you may place your check within …:

let 	startCount = hypeDocument.customData['startCountdownInput'] || 100,
		stopCount = hypeDocument.customData['stopCountdownInput'] || 0,
		duration = (Math.abs(startCount - stopCount)/100)*5000,
		countDownElement = hypeDocument.customData['countDownElement'],
		intervalTime = duration/Math.abs(startCount - stopCount),
		defaultPointerEvents = element.style.pointerEvents;
		
		element.style.pointerEvents = 'none';

				
		
let countDown = setInterval(

function(){

//reset
if(startCount === stopCount){
clearInterval(countDown);
element.style.pointerEvents = defaultPointerEvents;
}
//end reset


countDownElement.innerHTML = startCount;

if(startCount > stopCount)	{
startCount--
}else{
startCount++
}	


},
intervalTime
);
2 Likes

So repeating the codeblock is what actually powers the countdown? Do I understand this correctly?

yes, it runs as long as startCount is not equal to stopCount

1 Like