# Having number counter animation only count once

**URL:** <https://forum.wixstudio.com/t/having-number-counter-animation-only-count-once/52593>\
**Category:** Ask the community\
**Tags:** code\
**Created:** [September 22, 2023, 12:41pm UTC](https://forum.wixstudio.com/t/having-number-counter-animation-only-count-once/52593 "2023-09-22T12:41:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Oliver\_Larsen1](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/oliver_larsen1/32/82939_2.png) [@Oliver\_Larsen1](https://forum.wixstudio.com/u/Oliver_Larsen1)\
**Post date:** [September 22, 2023, 12:41pm UTC](https://forum.wixstudio.com/t/having-number-counter-animation-only-count-once/52593/1 "2023-09-22T12:41:08Z")

</div>

I need help with making the number counter animation count once. I have it onviewpoint, so it first starts counting when you have scrolled down to the section. Then i tried to add a **clearInterval()** I want it to only count once, and when you scroll away from the section and back that it didn’t keep start a new count. The below section doesn’t contain the code i tried to use to make it stop counting after the first time. Can anyone help me to add the code that would make it count only once, even when leaving and reentering the section.

```javascript
 $w.onReady(function () {

    function countElement(element, startValue, endValue, prefix = "", suffix = "") {
            
             const duration = 3000;
             const increment = (endValue - startValue) / (duration / 20);
             let currentValue = startValue;
			 
     
             const timer = setInterval(() => {
                 currentValue += increment;
                 $w(element).text = prefix + `${(Math.round(currentValue)).toLocaleString('da-DK')}${suffix}`;
     
                 if (currentValue >= endValue) {
					 $w(element).text = prefix + `${endValue.toLocaleString('da-DK')}${suffix}`;
                     clearInterval(timer);
                 }

             }, 20);
         }
     
     
  
     $w('#section2'). onViewportEnter(async () =>{
		 await countElement("#text33", 2000,6060 , "", "%");
		 await countElement("#text36", 2000,6260 , "", "%");
	 })
});

```

---

<div class="post-metadata">

**Author:** ![oakstreetassociates](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/oakstreetassociates/32/27732_2.png) [@oakstreetassociates](https://forum.wixstudio.com/u/oakstreetassociates)\
**Post date:** [September 22, 2023, 6:01pm UTC](https://forum.wixstudio.com/t/having-number-counter-animation-only-count-once/52593/2 "2023-09-22T18:01:21Z")

</div>

You can just create a new variable and activate it for the first time on viewport enter. Very simple actually. Something like this:

```javascript
// before on ready and outside any function
let renderTimer = false

```

For your event handler function (section 2 code) → add a if statement and you’re good to go

```javascript
$w('#section2').onViewportEnter(() => {
  if (!renderTimer) {
    countElement("#text33", 2000, 6060, "", "%");
    countElement("#text36", 2000, 6260, "", "%");
    renderTimer = true
  }
})

```

---

<div class="post-metadata">

**Author:** ![Oliver\_Larsen1](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/oliver_larsen1/32/82939_2.png) [@Oliver\_Larsen1](https://forum.wixstudio.com/u/Oliver_Larsen1)\
**Post date:** [September 23, 2023, 8:12am UTC](https://forum.wixstudio.com/t/having-number-counter-animation-only-count-once/52593/3 "2023-09-23T08:12:57Z")

</div>

I can’t get it to work. Everytime i leave the section and reenter the counting starts again.  
This is how the code looks after i tried to add your variable:

```javascript
 let renderTimer = false
 $w.onReady(function () {

    function countElement(element, startValue, endValue, prefix = "", suffix = "") {
            
             const duration = 3000;
             const increment = (endValue - startValue) / (duration / 20);
             let currentValue = startValue;
			 
     
             const timer = setInterval(() => {
                 currentValue += increment;
                 $w(element).text = prefix + `${(Math.round(currentValue)).toLocaleString('da-DK')}${suffix}`;
     
                 if (currentValue >= endValue) {
					 $w(element).text = prefix + `${endValue.toLocaleString('da-DK')}${suffix}`;
                     clearInterval(timer);
                 }

             }, 20);
         }
     
     
  
     $w('#section2'). onViewportEnter(async () =>{
         if (!renderTimer) {
             await countElement("#text33", 2000,6060 , "", "%");
             await countElement("#text36", 2000,6260 , "", "%");
             Rendertimer = true
         }
	 })
});

```

---

<div class="post-metadata">

**Author:** ![Oliver\_Larsen1](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/oliver_larsen1/32/82939_2.png) [@Oliver\_Larsen1](https://forum.wixstudio.com/u/Oliver_Larsen1)\
**Post date:** [September 23, 2023, 8:15am UTC](https://forum.wixstudio.com/t/having-number-counter-animation-only-count-once/52593/4 "2023-09-23T08:15:13Z")

</div>

I have fixed the issue. Thank you!
