开发者

how to prevent this function to be executed more than 2 times in x miliseconds?

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 t开发者_高级运维ried 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!


loading_already = true;

You have == which is boolean is operator, not assignment.


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

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

Here's an example page which shows how this is used in conjunction with scroll detection (look at second example)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜