jQuery 滚动函数无法识别变量的更改
我想在我的页面上实现一个 jQuery 滚动函数,只有当用户位于某个类别时,该函数才会在滚动到页面底部时在 div 上附加更多数据。
类别更改是在变量“katcheck”上进行的。仅当 katcheck == "k" 时才应进行追加。但是滚动函数一旦触发,就不会监听变量的变化。 或者换句话说:当 katcheck 更改为其他内容时,滚动函数仍然将 katcheck 保存为“k”。
这是代码片段:
var $start=8;
var $length=2;
$contentLoadTriggered = false;
$(window).scroll(function() {
if($(window).scrollTop() >= ($("#full-page-container").height() - $(window).height()) && $contentLoadTriggered == false && katcheck == "k")
{
alert(katcheck);
$contentLoadTriggered = true;
$.get('getgallery_by_kat2.php', { k_id:1, st:$start, ln:$length }, function(data) {
$(data).hide().appendTo(".imgmain").fadeIn();
$contentLoadTriggered = false;
$start=$start+2; $("div#descri").hide();
});
}
});
如何使滚动函数识别变量变化?我放入函数中的警报证明,即使所有其他函数都识别出更改,滚动函数也不会。
谢谢
I want to implement a jQuery scroll function onto my page which appends more data on a div when scrolled to the bottom of the page ONLY WHEN the user is on a certain category.
The category change is made on a variable "katcheck". The appending should only happen when katcheck == "k" and only then. But the scroll function once fired, won't listen to the change of the variable.
Or in other words: when katcheck gets changed to something else, the scroll function still has katcheck saved as "k".
here is the code snippet:
var $start=8;
var $length=2;
$contentLoadTriggered = false;
$(window).scroll(function() {
if($(window).scrollTop() >= ($("#full-page-container").height() - $(window).height()) && $contentLoadTriggered == false && katcheck == "k")
{
alert(katcheck);
$contentLoadTriggered = true;
$.get('getgallery_by_kat2.php', { k_id:1, st:$start, ln:$length }, function(data) {
$(data).hide().appendTo(".imgmain").fadeIn();
$contentLoadTriggered = false;
$start=$start+2; $("div#descri").hide();
});
}
});
How can I make the scroll function recognize the variable change? The alert I put into the function proves that even if all other functions recognize the change, the scroll function does not.
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ajax 查询 $.get() 异步运行。
这意味着需要时间来完成其职责。
您可以通过打开异步模式来确保它具有正确的数据。
像这样:
然后使用 jQuery.get( ... ); 执行 ajax 调用
然后再打开一次
The ajax query $.get() operates asynchronistically.
Meaning that will take it own time to complete it duties.
You can make sure that it has the correct data by turning of the async mode.
Like so:
and then perform your ajax calls using jQuery.get( ... );
then just turning it on again once