I'm working on a script that causes me to load more records from the database when the user reaches the end of page scrolling.
PHP / SQL is right, the problem is that jQuery does not correctly report the number of occurrences of class
on the screen.
I imagine the solution is to implement .on
somehow, but I do not handle a lot of jQuery and I'm hitting on it.
I explain my code below:
$(document).ready(function() {
var posicaoAtual = $(window).scrollTop();
var documentSize = $(document).height();
var sizeWindow = $(window).height();
$(window).scroll(function() {
posicaoAtual = $(window).scrollTop();
//Até aqui, apenas verifico se o usuário chegou ao final da página.
if (posicaoAtual >= (documentSize - sizeWindow)) {
//Agora que começa o sofrimento
// Conta a quantidade de vezes que a section.empresa aparece na tela
var qtdRegistros = $("#empresas section.empresa").length;
//Variável que utilizo no meu PHP
var idClientes = $("#idClientes").val();
// Faz requisição ajax
$.post("carrega-registros.php", {
qtdRegistros: qtdRegistros,
idClientes: idClientes
}, function(resposta) {
// Coloca o conteudo na DIV
$("#empresas").append(resposta);
});
}
});
$(window).resize(function() {
posicaoAtual = $(window).scrollTop();
documentSize = $(document).height();
sizeWindow = $(window).height();
});
});
[EDIT 1]
The whole code works fine. My return is new section class='empresa'
within div id="empresas"
.
BUT by var qtdRegistros = $("#empresas section.empresa").length;
, the variable qtdRegistros
only identifies the number of times class
empresa
appears in the original HTML. Disregarding what comes from append
. This causes my PHP to bring duplicate results, since qtdRegistros
is always the same.
I need to update length
according to new class
that comes in append
. But that does not happen.
Researching a lot I found something similar implementing on
, but I just could not use it. Can someone tell me a path?