如何防止该函数在 x 毫秒内执行超过 2 次?
当我到达滚动底部时,如何防止执行两次?
if ($(window).scrollTop() == $(document).height() - $(window).height()){
last_msg_funtion();
}
我看到的选项:
我尝试设置 var loading_already == false 以及类似:
if(loading_already==false){
loading_already = true;
last_msg_function();
setTimeout('refresh_loading',300);
}
where
function refresh_loading(){ loading_already = false; }
但没有成功,对此有什么建议吗?
谢谢!
how can i prevent this to be executed twice when i get to the bottom of the scroll?
if ($(window).scrollTop() == $(document).height() - $(window).height()){
last_msg_funtion();
}
Options i see:
I tried setting a var loading_already == false and, something like:
if(loading_already==false){
loading_already = true;
last_msg_function();
setTimeout('refresh_loading',300);
}
where
function refresh_loading(){ loading_already = false; }
But didn't work out, any suggestion for this?
thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正在加载_已经= true;
你有 == ,它是布尔运算符,而不是赋值。
loading_already = true;
You have == which is boolean is operator, not assignment.
您应该使用很棒的 jQuerythrottle/debounce 插件。使这种事情变得非常容易。
以下示例将确保每 250 毫秒调用 las_msg_function 不超过一次(假设您的滚动检测表达式正确);
这是示例页面,展示了如何结合使用带滚动检测(看第二个例子)
You should use the awesome jQuery throttle/debounce plugin. Makes this sort of thing very easy.
The following example will ensure that
las_msg_function
is called no more than once every 250 milliseconds (assuming your scroll detection expression is correct);Here's an example page which shows how this is used in conjunction with scroll detection (look at second example)