我正在加载facebook likebox滚动,但是每次我向上/向下滚动时,都会触发ajax,而我只希望ajax触发一次。这是代码:
jQuery(document).ready(function() {
jQuery(window).scroll(function() {
var scroll = jQuery(window).scrollTop();
if (scroll <= 770) {
jQuery.get('ajax/facebook.html', function(data) { jQuery('.fblikeajax').html(data);});
}
});
});
Unbind the event listener after it fires:
jQuery(document).ready(function() {
var scrollHandler = function() {
var scroll = jQuery(window).scrollTop();
if (scroll <= 770) {
jQuery.get('ajax/facebook.html', function(data) { jQuery('.fblikeajax').html(data);});
}
$(window).unbind('scroll', scrollHandler);
}
jQuery(window).scroll(scrollHandler);
});
I haven't tested that code, but it should be pretty close.
You could add in a variable that checks if the ajax has been fired:
jQuery(document).ready(function() {
var ajax_fired = false;
jQuery(window).scroll(function() {
var scroll = jQuery(window).scrollTop();
if ((scroll <= 770) && !ajax_fired) {
ajax_fired = true;
jQuery.get('ajax/facebook.html', function(data) {
jQuery('.fblikeajax').html(data);
});
}
});
});
Use JQuery's on()
and off()
$(document).on("scroll", scrollcheck);
var scrollcheck = function () {
var scroll = jQuery(window).scrollTop();
if (scroll <= 770) {
jQuery.get('ajax/facebook.html', function (data) {
jQuery('.fblikeajax').html(data);
});
$(document).off("scroll", scrollcheck);
}
}