简体   繁体   中英

Execute 'onscroll' function only once

SEE - https://codepen.io/pranavraghaw1/pen/BaNGRXY

I'm using the onscroll event in Javascript to animate my chart, made using 'Chart.js'.

The problem is, The function gets called every time I scroll on the page.

The approach that I'm using to calculate the Position of Display(the X-position when the graph-animation will run) is :

滚动调用函数的简单方法

One solution that I tried (for execting the function only once) is :

let run = true;  
 if (Ypos>play && run) {
   run = false;
   disp();
 }

SEE - https://codepen.io/pranavraghaw1/pen/BaNGRXY

I'm not experience in jQuery, so I'll need a solution in Javascript only.

your problem is here let run = true; this should be declared outside your´addEventListener´ not inside.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM