How do I make a JS function only run once per spam of calls

Redshade

I have been trying to make a function in jQuery to let a div move up faster than the rest of the page when you scroll down, and have it do the same for the opposite direction.

This works, but with a 1.5 second cooldown to avoid the spam from $(window).scroll(function(). So people have to wait 1.5 seconds when they join the site for it to work and then they have to wait 1.5 seconds to scroll up and down each time to see the effect work perfectly.

How would I go around fixing this delay? There should be no delay for good user experience. (The transition function is from a library and is not at fault here.)

var $intervalReady = false;
setInterval(function(){if($intervalReady==false){
    $intervalReady = true;
}}, 1500);

$( document ).ready(function() {
    var $elementBeginPos = $('#headertext').position();
    $(window).scroll(function(){
      var top = $(this).scrollTop();
      if(top > 60 && $intervalReady==true){
        $('#headertext').transition({y:'30%'});
        $intervalReady = false;
      }
      else if(top < 60 && $intervalReady==true){
        $('#headertext').transition({y:'-30%'});
        $intervalReady = false;
      }
    });
});
Eriks Klotins

Here is a working example: https://jsfiddle.net/37khp0ft/ Adjust to your needs.

In general, update the state on timer event, not on the scroll event.

JS part:

function updatePos(selector, multiplier)
{
   var scrollPos = $(document).scrollTop(); 
   $(selector).css({top: scrollPos * multiplier});
}
$(document).ready(function()
{
   setInterval(updatePos, 100,'#div', -1.1 );
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

how do I iterate only once per function call

How to make a function in Node JS run only once

How can I make the onMouseOver function run only once? | Html

How do I make this button to call the function only once?

Run JS SetTimeout function only once per domain

Can I run a function only once per process in linqpad?

How do I make a popup <div> display only once per device?

Using p5.js, when using a function inside of draw, how do I make it only go once?

How would I run a function on load in javascript once per a day?

How do I make my if statement run only once in while loop?

How to make this run only once?

How do i make the movement function for the player count only once so that they can be executed one by one?

How do I get useEffect to only run once in React.JS?

js only - run a function only once

How do I run redux -toolkit only once in React?

How do I get setInterval to only run once?

How do I run a PowerShell Workflow only once?

How do I make sure a HTTP request is only called once?

How do I make Python code to execute only once?

How do I make the event triggered by a key only happen once?

javascript run function only once per button click

Bash - How to make the loop run only once?

Qthread calls run only once

How do I make the observe function work more than once?

How do I execute a function only once in CoffeeScript

Does it matter if the function calls in this loop run concurrently? And if so, how do I make each wait until previous is done?

How to run part of a click function only once

How to have Function "FirstNumeric" be run only once?

How to run a function only once in react