Title: Execute/Stop Script
Last modified: August 30, 2016

---

# Execute/Stop Script

 *  [Teddy_c](https://wordpress.org/support/users/teddy_c/)
 * (@teddy_c)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/executestop-script/)
 * I would like to use this first script:
 *     ```
       var reachedFromTop = false;
       var reachedFromBottom = false;
       $(window).scroll(function() {
           //After scrolling 100px from the top...
           if ($(window).scrollTop() > 100 ) {
               if (!reachedFromTop) something();
               reachedFromTop = true;
           } else if (reachedFromTop && otherCondition) {
               if (!reachedFromBottom) somethingElse();
               reachedFromBottom = true;
           }
       });
       ```
   
 * to execute the script bellow once the user has scrolled down to my nav bar and
   stop the script once the user has scrolled back up to my nav bar.
 *     ```
       var didScroll;
       var lastScrollTop = 0;
       var delta = 5;
       var navbarHeight = $('.main-navigation').outerHeight();
   
       $(window).scroll(function (event) {
           didScroll = true;
       });
   
       setInterval(function () {
           if (didScroll) {
               hasScrolled();
               didScroll = false;
           }
       }, 250);
   
       function hasScrolled() {
           var st = $(this).scrollTop();
   
           // Make sure they scroll more than delta
           if (Math.abs(lastScrollTop - st) <= delta)
           return;
   
       // If they scrolled down and are past the navbar, add class .nav-up.
           if (st > lastScrollTop && st > navbarHeight) {
       // Scroll Down
               $('.main-navigation').removeClass('nav-down').addClass('nav-up');
           } else {
       // Scroll Up
               if (st + $(window).height() < $(document).height()) {
                   $('.main-navigation').removeClass('nav-up').addClass('nav-down');
               }
           }
   
           lastScrollTop = st;
       }
       ```
   
 * I have tried different set up, however, I can’t get it work. The error that is
   returned is simple, nothing happen. I don’t have any specific message showing
   up in firebug.
 * What I have tried so far to do is to replace the something/somethingElse in the
   first script by setInterval. Also, in my JavaScript file I place the first script
   at the beginning of my file.

The topic ‘Execute/Stop Script’ is closed to new replies.

## Tags

 * [javascript](https://wordpress.org/support/topic-tag/javascript/)
 * [script](https://wordpress.org/support/topic-tag/script/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 0 replies
 * 1 participant
 * Last reply from: [Teddy_c](https://wordpress.org/support/users/teddy_c/)
 * Last activity: [10 years, 11 months ago](https://wordpress.org/support/topic/executestop-script/)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
