Skip to content

Instantly share code, notes, and snippets.

@ArtemSites
Last active October 9, 2022 11:05
Show Gist options
  • Save ArtemSites/6c1b654f2a38990ef7297e06dd3e32e0 to your computer and use it in GitHub Desktop.
Save ArtemSites/6c1b654f2a38990ef7297e06dd3e32e0 to your computer and use it in GitHub Desktop.
Счетчик - таймер с выводом на экран | Counter - timer with display
initTimer(0, sectionStats3End, 2000, sectionStats3, '', ' лет');
function initTimer(start, end, duration, objTarget, prefix = false, postfix = false) {
timer();
function timer() {
let interval = Math.round(duration / Math.abs(start-end));
let timerName = setInterval(function() {
if (start>end) {
if (start <= end) {
clearInterval(timerName);
}
objTarget.text(newText(start,prefix,postfix));
start--;
} else {
if (start >= end) {
clearInterval(timerName);
}
objTarget.text(newText(start,prefix,postfix));
start++;
}
}, interval);
}
function newText(newText,prefix,postfix) {
if (prefix) {
newText=prefix+newText;
}
if (postfix) {
newText=newText+postfix;
}
return newText;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment