0

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!

Toni Michel Caubet
  • 19,333
  • 56
  • 202
  • 378

2 Answers2

2

loading_already = true;

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

Justin Thomas
  • 5,680
  • 3
  • 38
  • 63
1

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)

jessegavin
  • 74,067
  • 28
  • 136
  • 164