简体   繁体   中英

Run if statement only one time inside scroll event

I have this code, I tried to execute the if statement only one time. So the user will get only one alert show when getting to the middle of the page, after skipping the alert and keep scrolling he shouldn't get the alert again. so I added this PassedValue boolean variable but it doesn't get changed to true when the alert is shown.

 $f = $.noConflict(); $f(document).ready(function() { $f(window).scroll(function() { var scrollAmount = $f(window).scrollTop(); var documentHeight = $f(document).height(); var scrollPercent = (scrollAmount / documentHeight) * 100; var PassedValue = false; //$(".3amal").append(scrollPercent); if (scrollAmount > documentHeight * 0.3 && !PassedValue) { alert('the user scrolled once!'); PassedValue = true; } else { //alert('Nothing done nothing to save'); } }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="container"> <div id="sidebar"> <ul> <li><a id="aboutlink" href="#" style="position: sticky;top: 0;">auck</a></li> <li><a id="projectslink" href="#">Projects</a></li> <li><a id="resumelink" href="#">Resume</a></li> <li><a id="contactlink" href="#">Contact</a></li> </ul> </div> <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/> <div class="3amal">Hellow i'm here</div> </div><br/><br/><br/> <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/> <div id="content"> <div class="" id="about"> <p class="header">uck</p> <p class="info">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p> </div> <div class="sections" id="projects"> <p class="header">Projects</p> <p class="info">Projects</p> </div> <div class="sections" id="resume"> <p class="header">Resume</p> <p class="info">Resume</p> </div> <div class="sections" id="contact"> <p class="header">Contact</p> <p class="info">Contact</p> </div> </div> </div>

You can create an external variable and then check it in the if statement:

// somewhere outside your handler/function
let reviewed = false;

// inside
if(scrollAmount > documentHeight*0.3 && !PassedValue && !reviewed) {
  reviewed = true;
  // your logic here
}

The PassedValue should be out of the scroll event method scope. Everytime the code calls the scroll event, the var passedValue is created as it is inside the scroll event. If you make it as global variable, it should work.

$f = $.noConflict();

// Moved passedValue to a global variable
var PassedValue=false;

$f(document).ready(function() {
 $f(window).scroll(function() {
 var scrollAmount = $f(window).scrollTop();
 var documentHeight = $f(document).height();
 var scrollPercent = (scrollAmount / documentHeight) * 100;

 //$(".3amal").append(scrollPercent);
  if(scrollAmount > documentHeight*0.3 && !PassedValue){
alert('the user scrolled once!');
PassedValue=true;
  }else{
  //alert('Nothing done nothing to save');
  }
});
});

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