I want this function to run when it enters the viewport... but whenever I use "viewportEnter" it shows me an error

Here is the code I am using

let startNum = 0 ;
let endNum = 8417000 ;
const duration = 20 ;

$w . onReady ( function () {

setInterval (() => { 
    countUp (); 
},  duration ); 

});

function countUp () {

**if**  ( startNum  <=  endNum ) { 
    $w ( '#numberText' ). text  =  startNum . toLocaleString (); 
    startNum  +=  84170 ; 
} 

}

  1. I don’t see the viewportEnter event handler in your code.

  2. I don’t think it’s a good idea to change the text every 20 milliseconds, it’s almost invisible to human eye. Instead change it every 100 milliseconds (and make the increment bigger in accordance).

  3. I’d suggest to clear the interval once it reaches the endNum. There’s no point in having it run forever with no functionality.