Please Help - Event on scroll

Please Help - Event on scroll

Hi,
I have a problem to understand how to run my function properly.

My function is like this :

$(window).on('scroll', function(){

      x = 40;

      while ( x > -1){
             do something...
            x--;
      }

});

My mistake is, event if the while is not finish, if I finish to scroll, my function stop !
But I want the complete execution of my function.

So I want it start my fonction when I scroll and finish my function when x = -1. 
I try to use on('scrollStart'... but I function don't start !

Thanks for your help.
Senelar Xavier