Untitled

mail@pastecode.io avatar
unknown
javascript
2 years ago
662 B
1
Indexable
Never
var page = 2

window.onscroll = function(ev) {
  if ((window.innerHeight + window.pageYOffset) >= document.body.offsetHeight) {
      getData()
      var page = page + 1
  }
};

function getData(){
  var ajax = new XMLHttpRequest();

  ajax.open("POST","http://localhost:3000/?perPage=5&page="+ page.toString(),true);

  ajax.onreadystatechange = function() {
    if (this.readyState == 4) {
      if (this.status == 200) {
        var data = JSON.parse(this.responseText);
        data.forEach(function(post){
          $( ".postWall" ).append( "<h3>"+post["title"]+"</h3>" )
        })
      }
    }
  }
  var formData = new FormData();
  ajax.send(formData)
}