Good day, I have infinite scroll to bring the answers from the database and in each answer I have a button, but as I scroll to get more answers, there are some answers that by clicking on the button I saved two records and when you reach the last answer and return up to click on any other answer sends you 3 records and the response that follows sends you another 3 records with a single click and so you continue to increase the sending of duplicate records to the base of data and according fences doing scroll upwards you keep increasing the records in each answer.
This is the code of my scroll:
$(window).on("load", function(){
var base = 0;
var tope = 3;
var reachedmax = false;
var idConsulta = $("#idConsulta").val();
//console.log("idConsulta", idConsulta);
$(window).scroll(function () {
if ($(window).scrollTop() == $(document).height() - $(window).height())
getData();
});
$(document).ready(function(){
getData();
});
function getData() {
if (reachedmax)
return;
$.ajax({
method: "POST",
dataType: "text",
data: {
base: base,
tope: tope,
idConsulta: idConsulta,
},
success: function(respuesta){
if (respuesta == "reachedmax"){
reachedmax = true;
}else{
base += tope;
//console.log("base", base);
$(".results").append(respuesta);
}
}
});
}
})
This is the code of the button:
$(".boton").click(function(){
var idRespuesta = $(this).attr("idRespuesta");
//console.log("idRespuesta", idRespuesta);
var idUsuario = localStorage.getItem("usuario");
//console.log("idUsuario", idUsuario);
var rutaRespuesta = "general";
//console.log("rutaRespuesta", rutaRespuesta);
var valorRespuesta = 1.0;
if(idUsuario == null){
swal({
title: "Debe ingresar",
text: "Para poder calificar las respuestas",
type: "warning",
confirmButtonText: "¡Cerrar!",
closeOnConfirm: false
},
function(isConfirm){
if(isConfirm){
window.location = localStorage.getItem("rutaActual");
}
});
}else{
/*=============================================
VERIFICAR SI YA DIO CLIC
=============================================*/
var datos = new FormData();
datos.append("idRespuesta", idRespuesta);
datos.append("idUsuario", idUsuario);
datos.append("rutaRespuesta", rutaRespuesta);
$.ajax({
method: "POST",
data: datos,
cache: false,
contentType: false,
processData: false,
success:function(respuesta){
console.log("respuesta", respuesta);
if(respuesta == 0){
swal({
title: "No puede calificar mas de dos veces.",
type: "warning",
confirmButtonText: "¡Cerrar!",
closeOnConfirm: false
},
function(isConfirm){
if(isConfirm){
window.location = localStorage.getItem("rutaActual");
}
});
/*=============================================
GUARDAR DATOS
=============================================*/
}else{
$(this).addClass("btn-outline-primary active");
var datos = new FormData();
datos.append("idRespuesta", idRespuesta);
datos.append("idUsuario", idUsuario);
datos.append("rutaRespuesta", rutaRespuesta);
datos.append("valorRespuesta", valorRespuesta);
$.ajax({
method: "POST",
data: datos,
cache: false,
contentType: false,
processData: false,
success:function(respuesta){
console.log("respuesta", respuesta);
}
})
}