本来要写一个滚动条到底部自动加载的功能,if里面的判断表达式结果确实是true但是if里面的代码被执行5次。设置全局变量,return false.....都没法解决这个问题。求js大神解答。该怎么写才合适。
$(window).scroll(function(){
if (($(document).height())==($(window).height()+$(document).scrollTop())) {
alert('loading');//这段代码被执行5次,为什么?求大神解答
}
});
贴上我编辑器里面的内容:
$(window).scroll(function(){
$('#footer').html(($(document).height())+'jj'+($(window).height()+$(document).scrollTop()));
// return false;
if (($(document).height())==($(window).height()+$(document).scrollTop())) {
// if(!loading) return false;
loading++;//中断后续请求
// loadingStatus('你好');
$.post('http://www.baidu.com',{c:'dd'},function(data){
if(data.status){
}else{
}
},'json');
alert(loading);
}
});
这段代码是拷贝你上面代码的,没啥问题。
<!DOCTYPE html>
<html>
<head>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.js">
</script>
<script>
var loading=0;
$(document).ready(function(){
$(window).scroll(function(){
$('p').html(($(document).height())+'jj'+($(window).height()+$(document).scrollTop()));
// return false;
if (($(document).height())==($(window).height()+$(document).scrollTop())) {
// if(!loading) return false;
loading++;//中断后续请求
// loadingStatus('你好');
$.post('http://www.baidu.com',{c:'dd'},function(data){
if(data.status){
}else{
}
},'json');
alert(loading);
}
});
});
</script>
</head>
<body>
<p style="height:1000px">如果您点击我,我会消失。</p>
</body>
</html>