【问题标题】:how to prevent this function to be executed more than 2 times in x miliseconds?如何防止此函数在 x 毫秒内执行超过 2 次?
【发布时间】:2011-03-28 03:21:57
【问题描述】:

当我到达滚动底部时,如何防止它被执行两次?

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

在哪里

function refresh_loading(){ loading_already = false; }

但没有成功,有什么建议吗?

谢谢!

【问题讨论】:

    标签: javascript jquery double-submit-prevention


    【解决方案1】:

    loading_already = true;

    你有 == 这是布尔运算符,而不是赋值。

    【讨论】:

    • 我活生生地死记硬背,所以这是一个活生生的错误,谢谢。但在我的代码中还可以。它应该工作吗?
    【解决方案2】:

    你应该使用很棒的jQuery throttle/debounce plugin。让这类事情变得非常简单。

    以下示例将确保每 250 毫秒调用 las_msg_function 不超过一次(假设您的滚动检测表达式是正确的);

    if ($(window).scrollTop() == $(document).height() - $(window).height()) {
      $.throttle(250, las_msg_function);
    }
    

    这里是an example page,它展示了它如何与滚动检测结合使用(查看第二个示例)

    【讨论】:

    • j 未定义 [Interrumpir en este error] (function(b,c){var $=b.jQuery||b.C...d,e,false):a(d,f,e !==false)}})(这个);
    • 并且只有在类似的情况下才会执行:$.throttle(250, last_msg_funtion());
    • $.throttle(250, last_msg_funtion()); 的语法不正确。在此处阅读更多信息:benalman.com/code/projects/jquery-throttle-debounce/docs/files/…
    猜你喜欢
    • 2019-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多