javascript - 函数不仅调用一次
问题描述
我正在尝试execute only once
,但它正在执行多次。
alert
当用户在滚动时到达页面的 90% 时,我正在显示。但它多次显示警报。
我也试过:
- 通过将语句转换为
true
执行后。
但它没有用
文件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
}
}
});
解决方案
您总是每次都将其设置为,所以它总是运行excuted
。false
尝试这个:
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
}
}
});
推荐阅读
- arrays - C中数组内数组的增量
- javascript - 如何将数组传递给工作表(将数组传递给工作表时出错)
- python - 如何使用 Python 手动实现指数平滑?
- visual-studio-2019 - 如何在 Visual Studio 中查看活动栏?我重装了,每次都看不到
- twilio - twilio 对话 sdk 的同步对话错误
- java - 无法在 Windows 10 上卸载 Apache Netbeans 11.2
- chef-infra - 术语“Chef Infra Client”和“Chef Client”有什么区别
- java - 当我用java从谷歌驱动器下载文件时,我得到符号
- blazor - 如何在 Web 上部署 Blazor Web 应用程序?分步程序是什么?
- python - 乘法时R十进制精度发生变化