JSFiddle - React, Tailwind, and code Playground
by ashish shubham
HTML
<div class="container" id="container_id">
</div>
CSS
.container {
height: 300px;
width: 80%;
border: 1px solid black;
}
.item {
width: 50px;
height: 50px;
margin: 10px;
background-color: #d1d3d8;
}
JavaScript
function getContent(offset, num) {
// Do not change this method.
const content = [];
for( i=0; i<num; i++) {
content.push(offset + i);
};
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(content);
}, Math.random() * 2000);
})
}
var append_string = "" ;
$( window ).scroll(function() {
var document_height = $( document ).height();
var window_height = $( window ).height();
var scroll_top_height = $(window).scrollTop();
if(scroll_top_height > document_height - window_height){
getContent(2, 4).then(result => {append_string = "" ;
result.forEach(function(element){append_string = append_string + "<div class='item'>" + element + "</div>" ;});
$(".container").append(append_string) ;
console.log(append_string) ;
});
}
}) ;
//getContent(2, 4).then(result => {result.forEach(function(element){console.log(element) ;})});