首页 > 解决方案 > 函数不仅调用一次

问题描述

我正在尝试execute only once,但它正在执行多次。

alert当用户在滚动时到达页面的 90% 时,我正在显示。但它多次显示警报。

我也试过:

但它没有用

文件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
        }
    }
});

标签: javascripthtmljquery

解决方案


您总是每次都将其设置为,所以它总是运行excutedfalse

尝试这个:

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
        }
    }
});

推荐阅读