Home > Software design >  Function is not only calling once
Function is not only calling once

Time:11-10

I am trying to execute only once, But it is executing multiple times. I am showing alert when user reaches the 90% of the page while scrolling. But it is showing alert multiple times.

I have also tried :-

  • By turning statement to true after executed.

But it didn't worked.

page.html

var percent = 90;
var window_scrolled;

$(window).scroll(function() {
    window_scrolled = ($(document).height()/100)*90;

    if($(window).scrollTop()   $(window).height() >= window_scrolled) {
        var excuted = false
        if (!excuted) {
            alert("scrolled to bottom");
            excuted = true

        }
    }
});

Any help would be much Appreciated. Thank You

CodePudding user response:

You are always setting to false the excuted each time, so it always runs.

Try this:

var excuted = false
$(window).scroll(function() {
    window_scrolled = ($(document).height()/100)*90;
    if($(window).scrollTop()   $(window).height() >= window_scrolled) {
        if (!excuted) {
            alert("scrolled to bottom");
            excuted = true
        }
    }
});
  • Related