滚动时对数据库进行双重请求
Double request on the database when scrolling
这是滚动时调用请求的javascript
$(document).ready(function(){
var tempScrollTop, currentScrollTop = 0;
$(document).scroll(function(){
currentScrollTop = $(document).scrollTop();
var ids = new Array();
if (tempScrollTop < currentScrollTop )
{
var result = currentScrollTop % 100;
if(result == 0)
{
//console.log('scroll happened');
var items = $(".item");
var items_l = items.length;
for(var i = 0; i < items_l; i++)
{
ids[i] = parseInt(items[i].id.replace(/'D+/g,""));
}
ids = ids.sort();
var last_id = ids[0];
$.ajax({
url: "ajax/load",
type: "POST",
data: {last_id : last_id},
success: function(res){
$("#content").append(res);
}
});
}
}
/*else if (tempScrollTop > currentScrollTop )
{
var result = currentScrollTop % 100;
if(result == 0)
{
$("#content").text("Вверх запрос " + result);
}
}*/
tempScrollTop = currentScrollTop;
})
});
这就是控制器的方法:
public function ajaxLoad()
{
$last_id = intval($this->input->post("last_id"));
//$last_id++;
$db_data['query'] = $this->db->query("SELECT * FROM items WHERE id < ".$last_id." ORDER BY id DESC LIMIT 1");
$data['content'] = $this->load->view('item', $db_data, true);
echo $data['content'];
}
我有两个问题:
1) 我如何防止双重请求,有时会使两个相等的记录被输出?当我向下滚动时
2) 我如何使滚动记录的每100个像素都有附加,但问题是很难使currentScrollTol%100==0,我如何将其更改为正常??
这个怎么样?
var tempScrollTop, currentScrollTop = 0, isLoading = false;
$(document).scroll(function(){
[…狙击…]
var last_id = ids[0];
if ( !isLoading ) {
isLoading = true;
$.ajax({
url: "ajax/load",
type: "POST",
data: {last_id : last_id},
success: function(res){
$("#content").append(res);
isLoading = false;
},
error: function() { isLoading = false; } // just in case
});
[…等…]
当然,这只会让你一次加载一个东西,但至少不会锁定你的UI。
您也可以将last_id值存储在更高的范围内(有点像布尔值),并使用它来确保不会再次加载相同的东西。。。这两种方法都有缺点,但可能适合您的需要。
-m1
所以您有两个问题:
1) 你并不总是达到100%条件
2) 你有时不止一次达到100%条件
我建议:
$(document).ready(function(){
var tempScrollTop, currentScrollTop = 0;
var loaded = {};
loaded[0] = true; //assuming the first piece comes loaded
$(document).scroll(function(){
currentScrollTop = $(document).scrollTop();
var ids = new Array();
//this gives you a different int value for each 100 px section
//at 67px, you get 0, at 345px you get 3, etc
int section = Math.floor(currentScrollTop/100)
if(!loaded[section]) {
//prevents double request
loaded[section] = true;
var items = $(".item");
var items_l = items.length;
for(var i = 0; i < items_l; i++)
{
ids[i] = parseInt(items[i].id.replace(/'D+/g,""));
}
ids = ids.sort();
var last_id = ids[0];
$.ajax({
url: "ajax/load",
type: "POST",
data: {last_id : last_id},
success: function(res){
$("#content").append(res);
}
});
}
});
});
我还要注意的是,如果你在向下滚动一点后重新加载,这些解决方案中的任何一个,包括原始解决方案,都会出现一些问题,因为浏览器会跳到你滚动到的点,而不会调用滚动事件来生成高于该点的页面。但这超出了问题的范围。
相关文章:
- 如何在执行 Ajax 请求大量数据库更新时实现进度条
- MongoDB MonkAPI根据数据库请求之外可用的查找结果设置变量
- 同步数据库请求节点JS ORM
- 如何在不发出 HTTP 请求的情况下检测 SQL 数据库中的更改
- 将数据从异步请求添加到 WebSQL 数据库时出错
- 使用 ng-repeat同步数据库中请求的数据
- 使用 mySQL、Php 和 JS,我如何请求 dans 使用我在数据库中的数据
- XMLHTTP 请求的 ASP 文件中需要什么才能将数据保存到数据库
- 如何加快具有许多数据库请求的大型网页的速度
- jQuery:使用ajax请求中的所有数据更新数据库
- Node.js并请求2个数据库
- 分析数据库作业没有从http请求中获取所有结果
- 带有get请求的多个数据库查询
- 滚动时对数据库进行双重请求
- 自动通知在数据库的变化:类似于facebook的朋友请求
- Ajax请求、数据库和安全性
- MongoDB / mongab Remove请求在本地工作,但在现场时删除数据库
- 在Node.js中对sqlite数据库进行异步http请求
- TypeError: undefined不是一个函数GET请求/数据库调用
- 主干应用POST请求数据库